Quantcast
Viewing all articles
Browse latest Browse all 30

Answer by stackFan for In detail, how does the 'for each' loop work in Java?

Prior to Java 8, you need to use the following:

Iterator<String> iterator = someList.iterator();while (iterator.hasNext()) {    String item = iterator.next();    System.out.println(item);}

However, with the introduction of Streams in Java 8 you can do same thing in much less syntax. For example, for your someList you can do:

someList.stream().forEach(System.out::println);

You can find more about streams here.


Viewing all articles
Browse latest Browse all 30

Trending Articles