Java 8 Streams Java Java API
java.util.stream.IntStream
void forEach(IntConsumer action)
This terminal operation performs an 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.intstream;import java.util.stream.IntStream;public class ForEachExample { public static void main(String... args) { System.out.println("-- sequential --"); IntStream intStream = IntStream.range(1, 5); intStream.forEach(System.out::println); //parallel System.out.println("-- parallel --"); IntStream intStream2 = IntStream.range(1, 5); intStream2.parallel() .forEach(System.out::println); }}
-- sequential --1234-- parallel --3421