Results 1 to 9 of 9
Like Tree3Likes
  • 1 Post By jim829
  • 1 Post By gimbal2
  • 1 Post By jim829

Thread: Iterating over collections in Java 8

  1. #1
    yhqian99 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    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
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Iterating over collections in Java 8

    Does the IDE support Java 8? If so, is the Java compliancy level set correctly? Check your compiler settings for your
    IDE preferences.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Iterating over collections in Java 8

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Iterating over collections in Java 8

    Quote Originally Posted by DarrylBurke View Post
    2. List doesn't have a forEach(...) method.
    Actually, it does, indirectly. In Java 8 the designers stuck in a default forEach method into the Iterable class.

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: Iterating over collections in Java 8

    I don't want to throw salt on the wound, but it is even referenced in that first "recommended reading" link ;)
    DarrylBurke likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Iterating over collections in Java 8

    Well, they mention a forEach method. But the one they are talking about is used as a method of the Stream class. I have not looked to see
    if the default implementation of the Iterable version is the same as the one in the Stream class. But they appear to behave similarly.

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Iterating over collections in Java 8

    Hadn't noticed that. Thanks for pointing it out.

    /off to remove unneeded .stream().forEach(...)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Iterating over collections in Java 8

    And yes, the tutorial does not refer to Iterable#forEach. There is a note at the bottom of Lesson: Aggregate Operations (The Java™ Tutorials > Collections) that touches on the differences though.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    yhqian99 is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default Re: Iterating over collections in Java 8

    You are right, the Java compliancy level setting is not correct.

    I use MyEclipse 6, which support highest level is jdk 6. and I was not aware of that.

    The code works fine when I use jdk 8 directly (javac and java).

    Thanks a lot.

    yhqian99

Similar Threads

  1. Collections java
    By peliasrojo in forum New To Java
    Replies: 3
    Last Post: 05-24-2014, 10:44 PM
  2. java collections framework
    By mamoonrizwan in forum New To Java
    Replies: 1
    Last Post: 11-27-2011, 06:19 PM
  3. java collections
    By hemanth123 in forum New To Java
    Replies: 1
    Last Post: 10-24-2011, 09:00 AM
  4. Java collections
    By learn2java in forum Advanced Java
    Replies: 6
    Last Post: 05-01-2011, 04:12 AM
  5. Replies: 0
    Last Post: 11-14-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •