The 30 second answer
I collect with groupingBy on the department field, which gives a map from department name to the list of employees. The default map and lists have no type or mutability guarantees. A null department throws, and I can swap in TreeMap or a downstream collector like counting if they ask.
Map<String, List<Employee>> byDept = emps.stream()
.collect(Collectors.groupingBy(Employee::dept));Default result: HashMap / ArrayList-like — no guarantees on concrete type, mutability, or thread-safety. A null classifier result throws NullPointerException.
Variants: TreeMap::new for sorted keys; downstream mapping(Employee::name, toList()), counting(), or averagingDouble(...).
Null tip: groupingBy rejects a null key.
Sources:
Sources (0)
See inline citations in the answer above.