This intermediate operation returns an equivalent stream that is sequential. May return itself, either because the stream was already sequential, or because the underlying stream state was modified to be sequential.
package com.logicbig.example.longstream;
import java.util.stream.LongStream;
public class SequentialExample {
public static void main(String... args) {
System.out.println("-- default --");
LongStream stream = LongStream.of(4, 7, 9, 11, 13, 17);
stream.forEach(System.out::println);
System.out.println("-- sequential --");
LongStream stream2 = LongStream.of(4, 7, 9, 11, 13, 17)
.sequential();
stream2.forEach(System.out::println);
System.out.println("-- parallel --");
LongStream stream3 = LongStream.of(4, 7, 9, 11, 13, 17)
.parallel();
stream3.forEach(System.out::println);
}
}
Output
-- default --
4
7
9
11
13
17
-- sequential --
4
7
9
11
13
17
-- parallel --
11
13
4
17
9
7