Close

Java 8 Streams - LongStream.forEach Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.LongStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.LongStreamLongStreamLogicBig

Method:

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.

Examples


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




See Also