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

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>

The List<List> 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.

As always, the source code of all these examples is available over on GitHub.