Java 8 Stream API: Difference Between limit() and skip() with Example

Learn the difference between limit() and skip() in Java 8 Stream API with examples.
The main difference is that limit(n) returns the first n elements from a stream, while skip(n) skips the first n elements and processes the remaining elements.

limit() :- The limit(n) method is used to return a stream containing only the first n elements. It is commonly used when we want to restrict the output size from a stream.

skip() :- The skip(n) method is used to skip the first n elements from a stream and process the remaining elements. It is useful when we do not want to include some initial elements in the result.

Code Explanation of limit() and skip() Program

  1. First, we created a list of integer values using Arrays.asList().
  2. In the first stream operation, we used the limit(5) method.
  3. The limit(5) method returns only the first 5 elements from the list.
  4. Therefore, the output of limit(5) is: [2, 4, 10, 5, 20]
  5. In the second stream operation, we used the skip(4) method.
  6. The skip(4) method skips the first 4 elements from the list.
  7. After skipping the first 4 elements, the remaining elements are collected into a list.
  8. Therefore, the output of skip(4) is: [20, 15, 25, 6, 12, 56]
				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class LimitSkipExample {
    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(2, 4, 10, 5, 20, 15, 25, 6, 12, 56);

        List<Integer> lmt = numbers.stream()
                                   .limit(5)
                                   .collect(Collectors.toList());

        System.out.println("Using limit method: " + lmt);

        List<Integer> skp = numbers.stream()
                                   .skip(4)
                                   .collect(Collectors.toList());

        System.out.println("Using skip method: " + skp);
    }
}
				
			

Output :
Using limit method: [2, 4, 10, 5, 20]
Using skip method: [20, 15, 25, 6, 12, 56]

Use Case of limit vs skip in Java 8

A common use case of limit() and skip() is pagination.
For example, if we want to display records page by page, we can use both methods together.
In this below example:
• skip(5) skips the first 5 records.
• limit(5) returns the next 5 records.
This is useful when displaying data in pages like page 1, page 2, page 3, etc.

				
					List<Integer> pageDisplay = numbers.stream()
                                .skip(5)
                                .limit(5)
                                .collect(Collectors.toList());
				
			

FAQs on limit() and skip() in Java 8 :-

What is limit() in Java 8 Stream API?
The limit() method returns a stream containing only the first specified number of elements.
What is skip() in Java 8 Stream API?
The skip() method skips the first specified number of elements and returns the remaining elements from the stream.
What is the difference between limit() and skip() in Java 8?
The main difference is that limit() returns the first n elements, while skip() removes the first n elements and returns the remaining elements.
Can we use limit() and skip() together in Java 8?
Yes, we can use both methods together. They are commonly used for pagination in Java applications.
Is limit() a terminal operation in Java Stream?
No, limit() is not a terminal operation. It is an intermediate operation because it returns another stream.
Is skip() a terminal operation in Java Stream?
No, skip() is also an intermediate operation because it returns a stream after skipping the specified number of elements.