假设我有以下这个类:
public class Employee{
private int id;
private List<Car> cars;
//getters , equals and hashcode by id
}
public class Car {
private String name;
}
我有一个员工名单(相同的ID可能会重复):
List<Employee> emps = ..
Map<Employee, List<List<Car>>> resultMap = emps.stream().collect(
Collectors.groupingBy(Function.identity(),
Collectors.mapping(Employee::getCars, Collectors.toList());
这给了我一个
Map<Employee, List<List<Car>>>
,如何获取一个Map<Employee, List<Car>
(像一个平面列表)?
Employee::getCars
替换为e->new ArrayList<Car>(e.getCars())
。 - Eran