Map vs FlatMap:
Java 8 stream API provides map() and flatMap() methods. Both these methods are intermediate methods and returns another stream as part of the output.
- map() method use for transformation
- flatMap() used for transformation & flattering
- flatMap()-> map()+flatring
map():
- map() takes Stream<T> as input and return Stream<R>
- Stream<R> map (String<T> input)
- it's mapper function produces single value for each input value. hence it also called as one-to-one mapping.
flatMap():
- flatMap() takes Stream<Stream<T>> as input and return Stream<R>
- Stream<R> map (String<T> input)
- it's mapper function produces multiple value for each input value. hence it also called as one-to-many mapping.
| map() | flatMap() |
|---|---|
| It processes stream of values. | It processes stream of stream of values. |
| It does only mapping. | It performs mapping as well as flattening. |
| It’s mapper function produces single value for each input value. | It’s mapper function produces multiple values for each input value. |
| It is a One-To-One mapping. | It is a One-To-Many mapping. |
| Data Transformation : From Stream to Stream | Data Transformation : From Stream<Stream to Stream |
| 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. |
Let's See the below example for better understanding
Employee.java
import java.util.List;
public class Employee {
private int id;
private String name;
private String email;
private List<String> mobileNumbers;
public Customer() {
}
public Employee(int id, String name, String email, List<String> mobileNumbers) {
this.id = id;
this.name = name;
this.email = email;
this.mobileNumbers = mobileNumbers;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<String> getMobileNumbers() {
return mobileNumbers;
}
public void setMobileNumbers(List<String> mobileNumbers) {
this.mobileNumbers = mobileNumbers;
}
}
Repository.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Repository {
public static List<Employee> getAll() {
return Stream.of(
new Employee(001, "Nadeem", "Nadeem@localhost.com", Arrays.asList("737937955", "9812356")),
new Employee(002, "Shyam", "Shyam@localhost.com", Arrays.asList("65563865", "987654")),
new Employee(003, "Punneet", "Punneet@localhost.com", Arrays.asList("9412345", "43256")),
new Employee(004, "Kailash", "Kailash@localhost.com", Arrays.asList("9812345678", "1234456546"))
).collect(Collectors.toList());
}
}
MapFlatMapExample.java
import java.util.List;
import java.util.stream.Collectors;
public class MapFlatMapExample {
public static void main(String[] args) {
List<Employee> employee = Repository.getAll();
List<String> emails = employee.stream()
.map(employee -> employee.getEmail())
.collect(Collectors.toList());
System.out.println(emails);
List<List<String>> mobileNumbers = customers.
stream().map(employee -> employee.getMobileNumbers())
.collect(Collectors.toList());
System.out.println(mobileNumbers);
List<String> mobiles = customers.stream()
.flatMap(employee -> employee.getMobileNumbers().stream())
.collect(Collectors.toList());
System.out.println(mobiles);
}
}
Output:
Nadeem@localhost.com,Shyam@localhost.com,Punneet@localhost.com,Kailash@localhost.com 737937955,9812356,65563865,987654,9412345,43256,9812345678,1234456546
Top comments (0)