Prerequisite : Streams in Java8
The limit(long N) is a method of java.util.stream.Stream object. This method takes one (long N) as an argument and returns a stream of size no more than N. limit() can be quite expensive on ordered parallel pipelines, if the value of N is large, because limit(N) is constrained to return the first N elements in the encounter order and not just any n elements.
Syntax :
Stream<T> limit(long N) Where N is the number of elements the stream should be limited to and this function returns new stream as output.
Exception : If the value of N is negative, then IllegalArgumentException is thrown by the function.
Below are some examples to understand the implementation of the function in a better way.
Example 1 :
// Java code to show implementation // of limit() function import java.util.*; class GFG { // Driver code public static void main(String[] args) { // Creating a list of integers List<Integer> list = new ArrayList<Integer>(); // adding elements in the list list.add(-2); list.add(0); list.add(2); list.add(4); list.add(6); list.add(8); list.add(10); list.add(12); list.add(14); list.add(16); // setting the value of N as 4 int limit = 4; int count = 0; Iterator<Integer> it = list.iterator(); // Iterating through the list of integers while (it.hasNext()) { it.next(); count++; // Check if first four i.e, (equal to N) // integers are iterated. if (count > limit) { // If yes then remove all the remaining integers. it.remove(); } } System.out.print("New stream of length N" + " after truncation is : "); // Displaying new stream of length // N after truncation for (Integer number : list) { System.out.print(number + " "); } } } |
Output :
New stream of length N after truncation is : -2 0 2 4
Application :
// Java code to show the use of limit() function import java.util.stream.Stream; import java.util.ArrayList; import java.util.List; class gfg{ // Function to limit the stream upto given range, i.e, 3 public static Stream<String> limiting_func(Stream<String> ss, int range){ return ss.limit(range); } // Driver code public static void main(String[] args){ // list to save stream of strings List<String> arr = new ArrayList<>(); arr.add("geeks"); arr.add("for"); arr.add("geeks"); arr.add("computer"); arr.add("science"); Stream<String> str = arr.stream(); // calling function to limit the stream to range 3 Stream<String> lm = limiting_func(str,3); lm.forEach(System.out::println); } } |
Output :
geeks for geeks
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.string.replace() method in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.BitSet.set() method in Java
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.math.BigInteger.modInverse() method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Method Class | getName() Method in Java
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.

