Understanding Collectors.mapping()
in Java with Examples
Lets learn with me about Collectors.mapping().
1. Introduction
Collectors.mapping()
is a powerful feature introduced in Java 8 that allows transforming collected data within grouping or partitioning collectors.
2. Collectors.mapping()
– What It Does?
It is used inside Collectors.groupingBy()
or Collectors.partitioningBy()
to apply a transformation (mapping function) before collecting results.
3. Syntax of Collectors.mapping()
static <T, U, A, R> Collector<T, ?, R> mapping(
Function<? super T, ? extends U> mapper,
Collector<? super U, A, R> downstream
)
👉 Parameters:
mapper
→ Function to transform the input.downstream
→ Collector to collect transformed results.
👉 Returns:
A collector that applies mapping before accumulating results.
4. Examples of Collectors.mapping()
Example 1: Group Employees by Department & Collect Only Names
Without Collectors.mapping()
, we'd collect entire Employee
objects.
With mapping()
, we extract only names, reducing memory usage.
import java.util.*;
import java.util.stream.Collectors;
class Employee {
String name;
String department;
Employee(String name, String department) {
this.name = name;
this.department = department;
}
public String getName() { return name; }
public String getDepartment() { return department; }
}
public class MappingExample {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee("Alice", "HR"),
new Employee("Bob", "Engineering"),
new Employee("Charlie", "Engineering"),
new Employee("David", "HR"),
new Employee("Eve", "Finance")
);
// Group employees by department and collect only names
Map<String, List<String>> employeeNamesByDept = employees.stream()
.collect(Collectors.groupingBy(
Employee::getDepartment,
Collectors.mapping(Employee::getName, Collectors.toList()) // Extract only names
));
System.out.println(employeeNamesByDept);
}
}
🔹 Output:
{HR=[Alice, David], Engineering=[Bob, Charlie], Finance=[Eve]}
👉 Why Collectors.mapping()
?
✅ Collects only employee names instead of entire Employee
objects.
✅ Reduces memory overhead.
Example 2: Partition Students Based on Pass/Fail & Collect Names
import java.util.*;
import java.util.stream.Collectors;
class Student {
String name;
int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
public String getName() { return name; }
public int getMarks() { return marks; }
}
public class MappingPartitionExample {
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student("John", 85),
new Student("Mike", 40),
new Student("Lucy", 75),
new Student("Emma", 30)
);
// Partition students into pass/fail and collect only names
Map<Boolean, List<String>> passedStudents =
students.stream()
.collect(Collectors.partitioningBy(
s -> s.getMarks() >= 50, // Pass if marks >= 50
Collectors.mapping(Student::getName, Collectors.toList()) // Extract names
));
System.out.println("Passed: " + passedStudents.get(true));
System.out.println("Failed: " + passedStudents.get(false));
}
}
🔹 Output:
Passed: [John, Lucy]
Failed: [Mike, Emma]
👉 Why Collectors.mapping()
?
✅ Partitions students into pass/fail while extracting only names.
5. When to Use Collectors.mapping()
?
🔹 When grouping data but only need specific fields (e.g., names, IDs).
🔹 To improve memory efficiency by avoiding redundant object collection.
🔹 Inside groupingBy()
and partitioningBy()
to customize collected data.
Happy Learning :)