I have below class definition -
class Employee {
Long key;
String initials;
Integer age;
}
Below is the definition of IntWrapper
class -
class IntWrapper {
private Integer i;
}
I have collection of Employee objects in a list List<Employee> = ...
.
Now I want to create a Map<String, Integer>
where key is the initial and value is maximum age of employee with that initials.
I achieved this by below logic -
Map<String, IntWrapper> r2 = empList.stream().collect(Collectors.groupingBy(e -> e.getName(),
Collectors.collectingAndThen(Collectors.maxBy(Comparator.nullsLast(Comparator.comparingInt(Employee::getAge))), empOpt -> empOpt.map(x -> new IntWrapper(x.getAge())).orElseGet(() -> null) )));
Everything works fine here when there no employee with null initials. If there, it throws NullPointerException.
I found a solution to groupby with null keys in this SO post https://stackoverflow.com/a/22650233/2541276.
public static <T, A> Collector<T, ?, Map<A, List<T>>>
groupingBy_WithNullKeys(Function<? super T, ? extends A> classifier) {
return Collectors.toMap(
classifier,
Collections::singletonList,
(List<T> oldList, List<T> newEl) -> {
List<T> newList = new ArrayList<>(oldList.size() + 1);
newList.addAll(oldList);
newList.addAll(newEl);
return newList;
});
}
Now I don't know how to apply collectors on the resulting values because Collectors.toMap
doesn't take collector to apply on the resulting type.
Any help how can I achieve the same?