Java 8 Streams Java Java API
Interface:
java.util.stream.LongStream
Method:
LongStream 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.longstream;
import java.util.stream.LongStream;
public class LimitExample {
public static void main(String... args) { LongStream stream = LongStream.of(4, 7, 9, 11, 13, 17); stream.limit(4) .forEach(System.out::println); } }
Output4 7 9 11
For parallel stream package com.logicbig.example.longstream;
import java.util.stream.LongStream;
public class LimitExample2 {
public static void main(String... args) { LongStream stream = LongStream.of(4, 7, 9, 11, 13, 17); stream.parallel() .limit(4) .forEach(System.out::println);
} }
Output11 4 9 7
|
|