Java 8 Streams Java Java API
java.util.stream.LongStream
void forEach(LongConsumer action)
This terminal operation performs an LongConsumer action for each element of this stream.
For parallel stream, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism.
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); }}
-- sequential --1234-- parallel --3421