Collectors you will use weekly
public class Example {
public static void main(String[] args) {
Map<String, Long> countByDept = employees.stream()
.collect(Collectors.groupingBy(Employee::department, Collectors.counting()));
Map<String, List<String>> namesByDept = employees.stream()
.collect(Collectors.groupingBy(Employee::department,
Collectors.mapping(Employee::name, Collectors.toList())));
Map<Boolean, List<Employee>> split = employees.stream()
.collect(Collectors.partitioningBy(e -> e.salary() > 100_000));
Map<String, Employee> byId = employees.stream()
.collect(Collectors.toMap(Employee::id, e -> e,
(left, right) -> left));
String csv = employees.stream()
.map(Employee::name)
.collect(Collectors.joining(", ", "[", "]"));
}
}
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 1 section
- teeing, ofNullable, unmodifiable lists
Test yourself
Answer these before moving on — recall is what makes it stick.