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);
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?