Close

Java 8 Streams - DoubleStream.sequential Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.DoubleStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.DoubleStreamDoubleStreamLogicBig

Method:

DoubleStream sequential()

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.

Examples


package com.logicbig.example.doublestream;

import java.util.stream.DoubleStream;

public class SequentialExample {

public static void main(String... args) {

System.out.println("-- sequential --");
DoubleStream ds2 = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds2.sequential()//redundant as stream was already sequential by default
.forEach(System.out::println);

System.out.println("-- parallel --");
DoubleStream ds = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds.parallel()
.forEach(System.out::println);

}
}

Output

-- sequential --
1.0
1.2
2.0
2.4
3.0
-- parallel --
2.0
1.2
1.0
3.0
2.4




package com.logicbig.example.doublestream;

import java.util.stream.DoubleStream;

public class SequentialExample2 {

public static void main(String... args) {
DoubleStream ds = DoubleStream.empty();
System.out.println("parallel: " + ds.isParallel());
ds = ds.parallel();
System.out.println("parallel: " + ds.isParallel());
ds = ds.sequential();
System.out.println("parallel: " + ds.isParallel());
}
}

Output

parallel: false
parallel: true
parallel: false




See Also