This terminal operation performs an LongConsumer action for each element of this stream.
package com.logicbig.example.longstream;
import java.util.stream.LongStream;
public class ForEachExample {
public static void main(String... args) {
System.out.println("-- sequential --");
LongStream stream1 = LongStream.range(1, 5);
stream1.forEach(System.out::println);
System.out.println("-- parallel --");
LongStream stream2 = LongStream.range(1, 5);
stream2.parallel()
.forEach(System.out::println);
}
}
Output
-- sequential --
1
2
3
4
-- parallel --
3
4
2
1