Java 8 – Stream skip() and limit() methods

In this article, we will discuss Stream’s skip() and limit() methods in details with examples. Both methods used for different purposes and they complement each other well. Let us see each one along with examples.

1. Stream skip() method :

  • This Stream method is an intermediate operation which skip/discards first n elements of the given stream
  • skip() method returns new stream consisting of remaining elements after skipping specified n elements from the original stream
  • Specified n can’t be negative, otherwise IllegalArgumentException is thrown
  • If specified n is higher than the original size of the Stream then an empty Stream is returned
  • Stream skip() method is stateful which means it is interfering with other elements in the stream to skip specified n elements
  • Note :- Number of elements returned in the new stream after skipping is always less than the original stream elements
  • Method signature :- Stream<T> skip(long n)

2. Stream skip() method examples :

2.1 To skip first 2 numbers

  • A list contains 7 integer numbers
  • We are skipping first 2 numbers and printing remaining integer numbers in the encounter order using forEach() method
package net.bench.resources.stream.skip.example;

import java.util.Arrays;
import java.util.List;

public class StreamSkipMethod {

	public static void main(String[] args) {

		// list of integer numbers
		List<Integer> numbers = Arrays.asList(
				1, 2, 3, 4, 5, 6, 7
				);

		System.out.println("List of integers after "
				+ "skipping first 2 numbers :- \n");

		// skip first 2 numbers and print remaining
		numbers
		.stream()
		.skip(2)
		.forEach(System.out::println);
	}
}

Output:

List of integers after skipping first 2 numbers :- 

3
4
5
6
7

2.2 To skip first 2 Even numbers

  • A list contains first 10 natural numbers
  • We are skipping first 2 even numbers and printing remaining even numbers in the encounter order using forEach() method
package net.bench.resources.stream.skip.example;

import java.util.stream.Stream;

public class StreamSkipFirst2EvenNumbers {

	public static void main(String[] args) {

		System.out.println("List of integer after "
				+ "skipping first 2 EVEN numbers :- \n");

		// skip first 2 numbers and print remaining
		Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
		.filter(i -> i%2 == 0)
		.skip(2)
		.forEach(System.out::println);
	}
}

Output:

List of integer after skipping first 2 EVEN numbers :- 

6
8
10

3. Stream limit() method :

  • This Stream method is an intermediate operation which limits to first n elements of the given stream
  • limit() method returns new stream consisting of elements not longer than the specified max size in the encounter order
  • Specified n can’t be negative, otherwise IllegalArgumentException is thrown
  • Stream limit() method is stateful which means it is interfering as it has to keep the state of the items that are being picked up
  • limit() method doesn’t consume entire stream. As soon as, limit reaches the maximum number of elements it ignores the remaining elements. Hence, it is the short-circuit operation.
  • In all, limit() method is short-circuit stateful intermediate operation
  • Note :- Number of elements returned in the new stream after limiting is always less than the original stream elements
  • Method signature :- Stream<T> limit(maxSize n)

4. Stream limit() method examples :

4.1 To limit first 5 numbers

  • A list contains 7 integer numbers
  • We are limiting to first 5 numbers and printing to console using forEach() method in the encounter order
package net.bench.resources.stream.limit.example;

import java.util.Arrays;
import java.util.List;

public class StreamLimitMethod {

	public static void main(String[] args) {

		// list of integer numbers
		List<Integer> numbers = Arrays.asList(
				1, 2, 3, 4, 5, 6, 7
				);

		System.out.println("List of integer after "
				+ "limiting to first 5 numbers :- \n");

		// limit first 5 numbers and print to console
		numbers
		.stream()
		.limit(5)
		.forEach(System.out::println);
	}
}

Output:

List of integer after limiting to first 5 numbers :- 

1
2
3
4
5

4.2 To limit first 5 Even numbers

  • A list contains integer numbers from 1 to 15
  • We are filtering to get all even number and then limiting to first 5 Even numbers
  • Finally, printing to console using forEach() method in the encounter order
package net.bench.resources.stream.limit.example;

import java.util.stream.Stream;

public class StreamLimitToFirst5EvenNumbers {

	public static void main(String[] args) {

		System.out.println("List of integer after "
				+ "limiting to first 5 EVEN numbers :- \n");

		// skip first 2 numbers and print remaining
		Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
		.filter(i -> i%2 == 0)
		.limit(5)
		.forEach(System.out::println);
	}
}

Output:

List of integer after limiting to first 5 EVEN numbers :- 

2
4
6
8
10

5. skip() and limit() methods together :

  • As mentioned earlier, both these Stream methods are used for different purpose and they can complement each other
  • We can limit an infinite stream using limit() method and at the same time we can skip first few initial elements
  • Below example illustrates, generating infinite stream from 1 to n where we are skipping first 5 elements and after that limiting to 10 elements
  • Finally, pretty printing to console using forEach() method
package net.bench.resources.stream.limit.example;

import java.util.stream.Stream;

public class StreamSkipLimitTogether {

	public static void main(String[] args) {

		// skip & limit together
		Stream
		.iterate(1, i -> i + 2) // generate Stream
		.skip(5) // skip 5 elements 
		.limit(10) // limit to 10 elements
		.forEach(num -> System.out.print(" " + num)); // print
	}
}

Output:

11 13 15 17 19 21 23 25 27 29

References:

Happy Coding !!
Happy Learning !!

Java 8 - Stream peek() method
Java 8 - Stream min() and max() methods