With Java 8 you can use forEach to iterate a Stream.
Given the following ArrayList
1 2 3 4 | List<String> colors = new ArrayList<String>(); colors.add("red"); colors.add("green"); colors.add("blue"); |
Create a new Stream using the ArrayList.
Create Stream
1 | Stream<String> colorStream = colors.stream(); |
Now simply call forEach on your Stream and supply either a Method Reference or a Lambda.
Method Reference
1 | colorStream.forEach(System.out::println); |
or
Lambda
1 | colorStream.forEach(e -> System.out.println(e)); |
Both examples print the same output to the console using System.out.println
Console Output
1 2 3 | red green blue |