Core Java

Java Stream mapMulti() Example

With the release of Java 16, several new features and enhancements were introduced. One such feature is the mapMulti() method in the Stream API. Let us delve into understanding how to use the Java Streams mapMulti method using a practical example.

1. Understanding Java mapMulti

The mapMulti() method was introduced in Java 16 as part of the Stream API. It provides a more flexible way to transform elements in a stream compared to the traditional map() and flatMap() methods. The key advantage of mapMulti() is that it allows you to map a single element to multiple elements or no elements at all, using a more imperative style of programming.

1.1 Usage of mapMulti()

The mapMulti() method takes a BiConsumer as an argument. The first argument of the BiConsumer is the current element being processed, and the second argument is an instance of Consumer which can be used to add elements to the resulting stream. This allows for a more controlled and potentially complex transformation logic.

1.2 Syntax of mapMulti()

The syntax of the mapMulti() method is as follows:

Stream<T> mapMulti(BiConsumer<? super T, ? super Consumer<? super R>> mapper)

Where:

  • T: The type of the input elements.
  • R: The type of the output elements.
  • mapper: A BiConsumer that accepts an input element and a Consumer to which output elements are passed.

1.3 When to Use mapMulti()?

The mapMulti() method is particularly useful in scenarios where:

  • You need to map a single input element to multiple output elements. Unlike flatMap(), which requires returning a stream, mapMulti() allows you to directly add elements to the output stream.
  • You want to filter out some elements while processing others. With mapMulti(), you can decide conditionally which elements to add to the output stream.
  • You need to implement more complex transformation logic that involves multiple steps or conditions. The imperative style of mapMulti() makes it easier to handle complex logic compared to the functional approach required by flatMap().
  • You are dealing with a large dataset where performance matters. mapMulti() can be more efficient than flatMap() because it avoids the overhead of creating and merging intermediate streams.

1.4 When to Use mapMulti() instead of flatMap()?

While both mapMulti() and flatMap() can be used to transform elements in a stream, there are specific scenarios where mapMulti() might be a better choice:

  • Performance Optimization: mapMulti() can be more efficient as it avoids creating intermediate streams and directly adds elements to the output stream. This can be beneficial when working with large datasets.
  • Complex Transformation Logic: If the transformation logic is complex and requires multiple steps or conditions, mapMulti() provides a more readable and imperative style for handling such cases.
  • Conditional Element Addition: When you need to conditionally add elements to the output stream, mapMulti() offers a straightforward way to do this without the need for additional filtering or mapping steps.
  • Direct Element Mapping: If you want to map each element directly to multiple elements without wrapping them in streams, mapMulti() allows for a more intuitive approach compared to flatMap().

1.5 Advantages of mapMulti()

The mapMulti() method offers several advantages:

  • Flexibility: mapMulti() provides greater flexibility in mapping elements. It allows mapping a single input element to multiple output elements or no elements at all, which is not easily achievable with map() or flatMap().
  • Imperative Style: The imperative style of mapMulti() makes it easier to express complex transformation logic, especially when conditions and multiple steps are involved.
  • Performance: By avoiding the creation of intermediate streams, mapMulti() can be more efficient in terms of both time and memory usage, particularly with large datasets.
  • Conditional Output: mapMulti() allows for the conditional addition of elements to the output stream, providing more control over the resulting elements.
  • Readable Code: For complex mappings, mapMulti() can lead to more readable and maintainable code compared to the nested and often cumbersome code required by flatMap().

2. Code Example

Let’s look at a basic example of using mapMulti():

package com.jcg.example;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MapMultiExample {
    public static void main(String[] args) {
        List<String> words = List.of("hello", "world");
        
        List<Character> characters = words.stream()
            .mapMulti((word, consumer) -> {
                for (char c : word.toCharArray()) {
                    consumer.accept(c);
                }
            })
            .collect(Collectors.toList());
        
        System.out.println(characters);
    }
}

In this example, we have a list of words. We use mapMulti() to convert each word into its constituent characters, effectively flattening the stream in the process.

2.1 Code Breakdown

Let’s break down the example code to understand it better:

  • List<String> words = List.of("hello", "world");: We start with a list of strings.
  • words.stream(): We create a stream from the list of words.
  • mapMulti((word, consumer) -> {...}): We use mapMulti() to process each word. The lambda expression receives each word and a consumer to which we can add characters.
  • for (char c : word.toCharArray()) { consumer.accept(c); }: For each character in the word, we call consumer.accept(c) to add the character to the resulting stream.
  • .collect(Collectors.toList()): Finally, we collect the resulting stream of characters into a list.

2.2 Output

The output of the above code will be:

[h, e, l, l, o, w, o, r, l, d]

3. Conclusion

The mapMulti() method in Java 16 provides a powerful and flexible way to transform stream elements. It allows for more complex mappings than map() and flatMap(), making it a valuable addition to the Stream API. By understanding and utilizing mapMulti(), developers can write more concise and readable code for complex data transformations.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button