Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In Java Streams, processing and transforming data efficiently is crucial for effective programming. Two powerful techniques for handling successive elements are using SimpleEntry for straightforward pairing and stateful transformations for more dynamic scenarios.

In this tutorial, we’ll explore how to leverage SimpleEntry to create pairs of adjacent elements and learn how stateful transformations can provide a flexible approach to managing and processing data streams.

Let’s dive in to discover how these methods can enhance our Java Stream operations.

2. Problem Statement

Given a stream of elements, we want to create a list of pairs, where each pair consists of successive elements from the stream. For instance, given a stream of integers [1, 2, 3, 4, 5], we want to collect pairs [(1, 2), (2, 3), (3, 4), (4, 5)].

Let’s look at how we can approach the solution.

3. Solution Approach

To collect successive pairs from a stream in Java, we can use various approaches depending on the specific requirements and constraints of our task.

Let’s see two common ways to achieve this.

3.1. Collect Pairs Into List<SimpleEntry>

The approach starts with collecting all elements from the Stream into a List. This allows access to elements by index, which is necessary for pairing.

Let’s have a look at the implementation:

public static <T> List<SimpleEntry<T, T>> collectSuccessivePairs(Stream<T> stream) {
    List<T> list = stream.collect(Collectors.toList());
    return IntStream.range(0, list.size() - 1)
      .mapToObj(i -> new SimpleEntry<>(list.get(i), list.get(i + 1))).collect(Collectors.toList());
}

After collecting all elements, we create an IntStream of indices from 0 to list.size() – 2. This is because the last element doesn’t have a successive element to pair with. It’s worth noting that IntStream.range(0, list.size() – 1) generates indices from 0 up to but not including list.size() – 1.

Then, we map each index i to a SimpleEntry containing the element at index i and the element at index i + 1.

At last, we collect these SimpleEntry objects into a List.

So, moving on to time complexity, collecting the stream to a list has a time complexity of O(n), and pairing the elements using IntStream.range() has a time complexity of O(n). So, the overall time complexity is O(n). Additionally, the list of collected elements requires O(n) space and the list of pairs also requires O(n) space. So, the overall space complexity is O(n).

3.2. Collect Pairs Into List<List<T>>

The List<List<T>> approach offers a straightforward way to collect successive pairs of elements from a stream into nested lists.

Let’s have a look at the implementation first:

public static <T> Stream<List<T>> pairwise(Stream<T> stream) {
    List<T> list = stream.collect(Collectors.toList());
    List<List<T>> pairs = new ArrayList<>();
    for (int i = 0; i < list.size() - 1; i++) {
        pairs.add(Arrays.asList(list.get(i), list.get(i + 1)));
    }
    return pairs.stream();
}

The method pairwise() takes a Stream of elements and returns a Stream of successive pairs of elements as a List.

At first, we collect the elements from the input stream into a list. This allows for random access to elements by index:

Then we initialize the empty pair to store the successive pairs and loop over the list.

  • A loop iterates over the list, stopping at the second-to-last element
  • For each element list.get(i), it creates a pair with the next element list.get(i + 1)
  • The pair is added to the pairs list

At last, the method converts the list of pairs back into a stream and returns it.

Now, let’s discuss time and space complexity. At first, we are collecting the stream to a list with a time complexity of O(n) and pairing the elements with a time complexity of O(n). So, the overall time complexity is O(n). And, the list of collected elements requires O(n) space and the list of pairs also requires O(n) space. So, the overall space complexity is O(n).

4. Conclusion

In this article, we learned that SimpleEntry and stateful transformations offer valuable approaches for handling successive pairs in Java Streams. SimpleEntry provides a clean and concise way to create and manage pairs of adjacent elements, while stateful transformations offer flexibility and power in more complex data processing scenarios. By understanding and applying these techniques, we can optimize our stream operations and handle data more efficiently and clearly.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW