Java Streams - map() vs flatMap()
Introduction
In this block, we will learn about map and flatMap methods in Java streams API. Both are very useful intermediate methods of Java 8, to perform transformation operations on a list.
map() method used for transformation (e.g. converting lowercase strings to uppercase strings)
flatMap() used for transformation and flattening ( e.g converting a stream of lowercase strings to uppercase strings).
flatmap() -> map() + flattening.
Map() method
map() method is used for data transformation
map() takes Stream<T> as input and return Stream <R>
Stream<R> map(Stream<T> input){}
Its mapper function produces a single value for each input value hence it is called One-to-One mapping
<R> Stream<R> map(Function<? super T,? extends R> mapper);
flatMap() method
flatMap() takes Stream<Stream<T>> as input and return stream<R>
Stream<R> map(Stream<Stream<T>> input){}
<R> Stream <R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
Its mapper function produces multiple values for each input value. Hence it is called One-To-Many mapping.
Data Transformation and Flattening
Data Transformation
Data transformation means doing some operations on a list of data.
Ex:- Stream.of("a","b","c","d") -> [A,B,C,D] ( Transform data from lowercase to uppercase)
Flattening
Flattening means converting stream of stream into single stream.
Ex:- [[1,2],[3,4],[5,6],[7,8]] - [1,2,3,4,5,6,7,8]
Real-world example
Suppose there is a customer list containing the email id for each customer and a list of phone numbers for each customer.
We can use the map method to print the list of emails as there is one-to-one mapping whereas, in the case of phone numbers, there is a list so it will need flatMap.
Database.java
public static List<Customer> getAll(){
return Stream.of(
new Customer(101, "john", "john@gmail.com", Arrays.asList("397937955", "21654725")),
new Customer(102, "smith", "smith@gmail.com", Arrays.asList("89563865", "2487238947")),
new Customer(103, "peter", "peter@gmail.com", Arrays.asList("38946328654", "3286487236")),
new Customer(104, "kely", "kely@gmail.com", Arrays.asList("389246829364", "948609467"))
).collect(Collectors.toList());
}
MapvsFlatmap.java
public class MapvsFlatMap {
public static void main(String[] args) {
List<Customer> customers=EkartDatabase.getAll();
//List<Customer> convert List<String> -> Data transformation
//mapping: customer -> customer.getEmail()
//customer -> customer.getEmail() one to one mapping.
List<String> emails= customers.stream().map(customer -> customer.getEmail())
.collect(Collectors.toList());
System.out.println(emails);
//o/p:- [john@gmail.com, smith@gmail.com, peter@gmail.com, kely@gmail.com]
//customer->customer.getPhoneNumber() ->> one to many mapping
List<String> phones=customers.stream()
.flatMap(customer -> customer.getPhoneNumbers().stream())
.collect(Collectors.toList());
System.out.println(phones);
//o/p:- [397937955, 21654725, 89563865, 2487238947, 38946328654,
// 3286487236, 389246829364, 948609467]
}
}
Differences
map() | flatMap() |
It processes a stream of values. | It processes a stream of stream of values. |
It does only mapping | It performs mapping as well as flattering. |
Its mapper function produces a single value for each input value. | Its mapper function produces multiple values for each input value. |
It is a one-to-one mapping. | It is a One-To-Many mapping. |
Use this method when the mapper function is producing a single value for each input value. | Use this method when the mapper function is producing multiple values for each input value. |
Summary
In this post, we have learned about map vs flatMap in Java 8.
Thank you for reading. If you found this article helpful, react and share. Cheers.