In this post, we will learn the difference between Map() and flatMap() in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Difference between Map() and flatMap() in Java
Features | Map() | flatMap() |
---|---|---|
Stream Processing | It processes a stream of values | It processes a stream of stream of values |
Operation | It only does mapping | It performs both mapping and flattening |
Mapper Function | Its mapper function produces a single value for each input value | Its mapper function produces multiple values for each input value |
Mapping | It is a One-To-One mapping | It is a One-To-Many mapping |
Data Transformation | From Stream<T> to Stream<R> | From Stream<Stream<T>> to Stream<R> |
Use Case | 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 |
Suppose we have a list of students, and each student has a list of courses they have taken. We want to perform the following operations:
- Using map(): Get a list of all courses taken by all students.
- Using flatMap(): Get a list of distinct courses taken by all students.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Student {
private String name;
private List<String> courses;
public Student(String name, List<String> courses) {
this.name = name;
this.courses = courses;
}
public String getName() {
return name;
}
public List<String> getCourses() {
return courses;
}
}
public class MapVsFlatMapExample {
public static void main(String[] args) {
Student student1 = new Student("Alice", Arrays.asList("Math", "History", "Science"));
Student student2 = new Student("Bob", Arrays.asList("Math", "English", "Geography"));
Student student3 = new Student("Charlie", Arrays.asList("History", "Biology", "Chemistry"));
List<Student> students = Arrays.asList(student1, student2, student3);
// Using map() to get a list of all courses taken by all students
List<List<String>> allCourses = students.stream()
.map(student -> student.getCourses())
.collect(Collectors.toList());
System.out.println("Using map():");
System.out.println(allCourses);
// Using flatMap() to get a list of distinct courses taken by all students
List<String> distinctCourses = students.stream()
.flatMap(student -> student.getCourses().stream())
.distinct()
.collect(Collectors.toList());
System.out.println("\nUsing flatMap():");
System.out.println(distinctCourses);
}
}
Output:
Using map():
[[Math, History, Science], [Math, English, Geography], [History, Biology, Chemistry]]
Using flatMap():
[Math, History, Science, English, Geography, Biology, Chemistry]
Explanation:
With map(), we get a list of lists, where each inner list represents the courses taken by each student.
With flatMap(), we first flatten the list of lists into a single stream of courses and then use distinct() to get the distinct courses taken by all students.
As seen in the output, map() preserves the structure of the original list, while flatMap() flattens the list of lists into a single list, making it useful for scenarios where we want to work with a single stream of elements rather than nested lists.
Interview Questions
Java
Stream
X vs Y
Comments
Post a Comment