Close

Java 8 Streams - DoubleStream.limit Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.DoubleStream

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

Method:

DoubleStream limit(long maxSize)

This intermediate operation returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

This is a short-circuiting stateful operation.

Examples


package com.logicbig.example.doublestream;

import java.util.stream.DoubleStream;

public class LimitExample {

public static void main(String... args) {
System.out.println("-- sequential --");
DoubleStream ds = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds.limit(3)
.forEach(System.out::println);


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

Output

-- sequential --
1.0
1.2
2.0
-- parallel --
2.0
1.0
1.2




package com.logicbig.example.doublestream;

import java.util.stream.DoubleStream;

public class LimitExample2 {

public static void main(String... args) {
DoubleStream ds = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds.limit(10000)
.forEach(System.out::println);
}
}

Output

1.0
1.2
2.0
2.4
3.0




See Also