Quantcast
Channel: In detail, how does the 'for each' loop work in Java? - Stack Overflow
Viewing all articles
Browse latest Browse all 30

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

$
0
0

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



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>