是否有可能转换List
为Map
一个lambda表达式?
public class Entry { Employee employee; LocalDate date; }
到目前为止,我想出了类似的东西:
entries.stream().collect(Collectors.toMap(Collectors.groupingBy(Entry::getEmployee), Collectors.groupingBy(Entry::getDate), Function.identity()));
但这会产生编译错误:
no suitable method found for toMap(java.util.stream.Collector>>??,java.util.stream.Co??llector >>, java.util.func??tion.Function )
谢谢
假设Entry
有getter和Employee
overrides hashCode()
并且equals()
:
Map> result = entries.stream() .collect(Collectors.groupingBy(Entry::getEmployee, Collectors.toMap(Entry::getDate, Function.identity())));
请注意,如果员工有重复日期,这将抛出异常.