Wednesday, July 3, 2024
HomeLanguagesJavaLongStream limit() in Java

LongStream limit() in Java

LongStream limit(long maxSize) returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

Note : LongStream 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 :

LongStream limit(long maxSize)

Parameters :

  1. LongStream : A sequence of primitive long-valued elements. This is the long primitive specialization of Stream.
  2. 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 LongStream limit
// (long maxSize)
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream
        LongStream stream = LongStream.of(2L, 4L, 6L, 8L, 10L);
  
        // Using LongStream 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
4
6

Example 2 :




// Java code for LongStream limit
// (long maxSize)
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream of numbers [5, 6, .. 11]
        LongStream stream = LongStream.range(5, 12);
  
        // Using LongStream 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 :

5
6
7
8

Example 3 :




// Java code for LongStream limit
// (long maxSize)
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream
        LongStream stream = LongStream.iterate(4L, num -> num + 2);
  
        // Using LongStream 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
6
8
10

Difference between LongStream limit() and LongStream skip() :

  1. 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.
  2. 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.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments