DoubleStream limit() in Java with Examples
DoubleStream limit(long maxSize) returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
Note : DoubleStream limit() is a short-circuiting stateful intermediate operation i.e, when processed with an infinite input, it may produce a finite stream as a result without processing the entire input.
Syntax :
DoubleStream limit(long maxSize)
Parameters :
- DoubleStream : A sequence of primitive double-valued elements. This is the double primitive specialization of Stream.
- maxSize : The number of elements the stream should be limited to.
Return Value : The function returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
Exception : The function throws IllegalArgumentException if maxSize is negative.
Example 1 :
// Java code for DoubleStream limit // (long maxSize) import java.util.*; import java.util.stream.DoubleStream; class GFG { // Driver code public static void main(String[] args) { // Creating an DoubleStream DoubleStream stream = DoubleStream.of(2.3, 4.4, 6.7, 8.9, 10.5); // Using DoubleStream limit(long maxSize) to // get a stream consisting of the elements of // this stream, truncated to be no longer // than maxSize in length. stream.limit(3).forEach(System.out::println); } } |
Output :
2.3 4.4 6.7
Example 2 :
// Java code for DoubleStream limit // (long maxSize) import java.util.*; import java.util.stream.DoubleStream; class GFG { // Driver code public static void main(String[] args) { // Creating an DoubleStream DoubleStream stream = DoubleStream.iterate(4.5, num -> num + 3); // Using DoubleStream limit(long maxSize) to // get a stream consisting of the elements of // this stream, truncated to be no longer // than maxSize in length. stream.limit(4).forEach(System.out::println); } } |
Output :
4.5 7.5 10.5 13.5
Difference between DoubleStream limit() and DoubleStream skip() :
- The limit() method returns a reduced stream of first maxSize elements but skip() method returns a stream of remaining elements after skipping first maxSize elements.
- limit() is a short-circuiting stateful intermediate operation i.e, when processed with an infinite input, it may produce a finite stream as a result without processing the entire input but skip() is a stateful intermediate operation i.e, it may need to process the entire input before producing a result.
Recommended Posts:
- DoubleStream min() in Java with examples
- DoubleStream max() in Java with examples
- ByteBuffer limit() methods in Java with Examples
- ShortBuffer limit() method in Java with Examples
- LongBuffer limit() method in Java with Examples
- IntBuffer limit() method in Java with Examples
- DoubleBuffer limit() methods in Java with Examples
- FloatBuffer limit() Method in Java with Examples
- CharBuffer limit() methods in Java with Examples
- Buffer limit() methods in Java with Examples
- DoubleStream allMatch() in Java with examples
- DoubleStream peek() in Java with examples
- DoubleStream boxed() in Java with Examples
- DoubleStream anyMatch() in Java with examples
- DoubleStream distinct() in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



