How has enhanced for-each loop changed in recent Java versions?

The enhanced for-each loop, introduced in Java 5, allows for simplified iteration over collections and arrays. Over recent Java versions, enhancements in the collections framework and the introduction of features like the Streams API in Java 8 have made the enhanced for-each loop even more powerful and flexible.

Although the syntax of the enhanced for-each loop remains the same, its capabilities have been boosted by improvements in the Java Collections Framework and new features that come with the Java language. For example, the use of lambda expressions and method references in conjunction with the Streams API has made it easier to process collections without explicit loops.

Here is an example of the enhanced for-each loop in action:

List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry"); for (String fruit : fruits) { System.out.println(fruit); }

Enhanced for-each loop Java 5 Streams API lambda expressions Java Collections Framework