DEV Community

realNameHidden
realNameHidden

Posted on

3 1 1 1 1

How does the limit() method differ from the skip() method in streams?

The limit() and skip() methods in Java Streams are used to control the number of elements in a stream, but they serve different purposes:

1. limit()

The limit(n) method is used to truncate the stream to the first n elements.

Key Characteristics:

  • Keeps only the first n elements of the stream.
  • Stops processing after n elements, making it useful for performance optimizations with large streams.
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
List<Integer> limited = numbers.stream()
                               .limit(3)
                               .collect(Collectors.toList());
System.out.println(limited); // Output: [1, 2, 3]

Enter fullscreen mode Exit fullscreen mode

Use Cases:

  • Fetching a small subset of data (e.g., top 10 results).
  • Paging or displaying limited records.

2. skip()

The skip(n) method is used to discard the first n elements of the stream and return the remaining elements.

Key Characteristics:

  • Skips the first n elements.
  • The resulting stream starts from the (n+1)th element.

Example

List<Integer> numbers = List.of(1, 2, 3, 4, 5);
List<Integer> skipped = numbers.stream()
                               .skip(2)
                               .collect(Collectors.toList());
System.out.println(skipped); // Output: [3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

Use Cases:

  • Ignoring initial elements in processing.
  • Paging or scrolling data (e.g., skipping records for pagination).

Using Both Together

You can combine limit() and skip() for scenarios like pagination, where you need to fetch a specific range of elements.

Example: Pagination

Suppose you have a list of elements and want to fetch page 2 with a page size of 3 (elements 4–6).

List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
int page = 2;
int pageSize = 3;

List<Integer> pageData = numbers.stream()
                                .skip((page - 1) * pageSize) // Skip the first 3 elements
                                .limit(pageSize)             // Fetch the next 3 elements
                                .collect(Collectors.toList());
System.out.println(pageData); // Output: [4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

Key Points to Remember

  • limit() is for taking a subset of elements from the beginning of the stream.

  • skip() is for skipping elements from the beginning of the stream.

  • Both methods are intermediate operations, meaning they can be chained with other stream methods.

  • They are particularly useful in data slicing, such as implementing pagination or controlling data flow in large datasets.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay