Java 8: From a for-loop to forEach statement 📎
Ordinary for loops:
List<String> strings = new ArrayList<>();
for (String string : strings) {
System.out.println("Content: " + string);
}
...can be easily translated into a forEach statement:
List<String> strings = new ArrayList<>();
strings.stream().forEach((string) -> {
System.out.println("Content: " + string);
});
With the "functional looping style" any pre- or post-processing like filtering, grouping or even parallelization can be easily achieved:
strings.parallelStream().
filter(s -> s.contains("java")).
forEach((string) -> {
System.out.println("Content: " + string);
});
}
NetBeans 8 converts ordinary for-loops into the functional notation by hitting the ALT+ENTER
key on, or by clicking on the yellow bulb.
See you at Java EE Workshops at MUC Airport or on demand and in a location very near you: airhacks.io!