Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Iterating over collections in Java 8

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Iterating over collections in Java 8

    Hi all,

    Today I installed jdk1.8.0_20 on my computer and typed a simple example code in MyEclipse 6.0 IDE, the code listed below:


    import java.util.List;

    public class Test5
    {
    public static void main(String[] args)
    {
    List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
    features.forEach(n -> System.out.println(n));
    }
    }


    the MyEclipse Editor shows some errors like "Arrays can not be resolved" and "n can not be resolved".
    I did use jdk 8 in the build path, but it seems like the jdk 8 did not function properly.

    I don't know what's the problem. Any idea? pls help.

    yhqian99


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Iterating over collections in Java 8

    The class Arrays needs to be imported before you can use it. That has been the same in all versions of java.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Iterating over collections in Java 8

    Please post your code correctly using code or highlight tags which are explained near the top of this link.

  4. #4

    Default Re: Iterating over collections in Java 8

    Using an Iterator to iterate over a List of strings.


    List names = new LinkedList();

    // ... add some names to the collection

    Iterator i = names.iterator();
    while (i.hasNext())
    {
    String name = (String) i.next();
    System.out.println(name);
    }

  5. #5
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Iterating over collections in Java 8

    n would be in your list and using List interface. Generics. Depending what your list is, in IDE as eclipse, it must actually read or have a list to iterate your program and complete it, using generics.

  6. #6
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 9 Times in 6 Posts

    Default Re: Iterating over collections in Java 8

    Anytime you have a collection of things you will need some mechanism to systematically step though the items in that collection. As an everyday example, consider the television remote control, which lets us iterate over various television channels. Similarly, in the programming world, we need a mechanism to systematically step through a collection of software objects. The mechanism used for this purpose is known by various names, including index (for iterating over an array), cursor (for iterating over the results of a database query), enumeration (in early versions of Java), and iterator (in more recent versions of Java).

    I first learned to program in an early version of FORTRAN, where the only data structuring capability was an array. I quickly learned how to iterate over an array using an index and a DO-loop. From there it was only a short mental leap to the idea of using a common index into multiple arrays to simulate an array of records. Most programming languages have features similar to arrays, and they support straightforward looping over arrays. But modern programming languages also support more complex data structures such as lists, sets, maps, and trees, where the capabilities are made available via public methods but the internal details are hidden in private parts of the class. Programmers need to be able to traverse the elements of these data structures without exposing their internal structure, which is the purpose of iterators.

    The Iterator pattern
    An iterator is a mechanism that permits all elements of a collection to be accessed sequentially, with some operation being performed on each element. In essence, an iterator provides a means of "looping" over an encapsulated collection of objects. Examples of using iterators include

    Visit each file in a directory (aka folder) and display its name.
    Visit each node in a graph and determine whether it is reachable from a given node.
    Visit each customer in a queue (for instance, simulating a line in a bank) and find out how long he or she has been waiting.
    Visit each node in a compiler's abstract syntax tree (which is produced by the parser) and perform semantic checking or code generation. (You could also use the Visitor pattern in this context.)

Similar Threads

  1. Help making and iterating through ArrayList (Java Beginner)
    By Sammis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 20th, 2013, 06:58 AM
  2. Java Collections and Log4j
    By francisolie in forum Java Theory & Questions
    Replies: 0
    Last Post: June 14th, 2013, 02:17 AM
  3. Replies: 4
    Last Post: May 28th, 2013, 11:24 PM
  4. [SOLVED] Iterating through an ArrayList
    By Actinistia in forum Collections and Generics
    Replies: 2
    Last Post: October 31st, 2011, 02:19 PM
  5. Iterating a String
    By av8 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: July 7th, 2011, 12:37 PM