Home  >  Java 8

Java Stream limit()

By Arvind Rai, May 17, 2020
The limit method of Stream returns a new stream consisting the elements of this stream truncated to given max size in length. The limit method consists the first n elements where n is less or equal to given max size.
Find the limit method declaration from Java doc.
Stream<T> limit(long maxSize) 
The parameter maxSize is the max number of elements in the stream returned by limit.
The limit method returns new Stream.
The method throws IllegalArgumentException if the parameter maxSize is -ve.

The limit method is a short-circuiting stateful intermediate operation.
Stream operations are divided into intermediate and terminal operations and are combined to form stream pipelines. An intermediate operation is a short-circuiting if it may produce a finite stream for infinite input. Intermediate operations are divided into stateless and stateful operations. Stateless operations retain no state from previously seen element when processing a new element such as filter and map. Stateful operations may incorporate state from previously seen elements when processing new elements such as distinct and limit.

Note: The limit method is a cheap operation on sequential stream pipelines and can be expensive for ordered parallel pipelines, especially for large value of maxSize. This is because limit(maxSize) is bound to return first maxSize elements in encounter order and not any element of maxSize number.

Now we will discuss the limit method examples.

Example-1:
Suppose we have a stream of numbers and we are calling limit method on it.
Stream.of(11, 12, 13, 14, 15).limit(3)
   .forEach(s->System.out.println(s)); 
The source stream has 5 numbers and we have called limit(3) with max size 3. Hence our output will be up to 3 elements starting from first element i.e. 11 12 13.

If max size of limit is greater than the number of elements, then all the elements of stream will be selected by limit method. Find the code below.
Stream.of("A", "B", "C", "D").limit(10)
   .forEach(s->System.out.println(s)); 
We can see that source stream has 4 elements whereas we are calling limit with max size 10. Here limit will return stream with all elements and the output will be A B C D.

Example-2:
Find the more examples of limit method.
LimitDemo2.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class LimitDemo2 {
  public static void main(String[] args) {
	System.out.println("--- code 1 ---");    
	List<String> list = Arrays.asList("Vijay", "Suresh", "Vinod");
	list.stream()
	  .map(e -> "Mr. " + e)
	  .limit(2)
	  .forEach(s->System.out.printf("%s ", s));

	System.out.println("\n--- code 2 ---");
	List<Integer> numList = Arrays.asList(31, 32, 33, 34);
	numList.stream()
	  .mapToInt(i -> i * 10)
	  .limit(3)
	  .forEach(s->System.out.printf("%s ", s));
  }
} 
Output
--- code 1 ---
Mr. Vijay Mr. Suresh 
--- code 2 ---
310 320 330 

Example-3:
Find the examples of limit method with parallel stream.
LimitDemo3.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class LimitDemo3 {
  public static void main(String[] args) {
	System.out.println("--- code 1 ---");    
	List<String> list = Arrays.asList("Varanasi", "Madurai", "Agartala");
	list.parallelStream()
	  .limit(2)
	  .forEach(s->System.out.printf("%s ", s));

	System.out.println("\n--- code 2 ---");
	List<Integer> numList = Arrays.asList(41, 42, 43, 44);
	numList.parallelStream()
	  .mapToInt(i -> i * 10)
	  .limit(3)
	  .forEach(s->System.out.printf("%s ", s));
  }
} 
Output
--- code 1 ---
Madurai Varanasi 
--- code 2 ---
420 410 430 

Reference

Java doc: Stream
POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us