Consider:
List<String> someList = new ArrayList<String>();
// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) {
System.out.println(item);
}
What would the equivalent for
loop look like without using the for each syntax?
Prior to Java 8, you need to use the following:
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:You can find more about streams here.
The for-each loop in Java uses the underlying iterator mechanism. So it's identical to the following: