[英]How do I convert a List<Employee> to Map<Employee, List<String>> using java8?
[英]Method to convert `List<Employee>` to `Map<Integer,List<Employee>>`
如何将List<Employee>
转换为Map<Integer,List<Employee>>
。
基于employee Object中存在的depId的 Group List<Employee>
, 映射键为depId。
java.util。*或Google Guava中是否有任何方法可以在不迭代列表的情况下进行转换。
在Java 8+中,您可以按ID使用流和分组:
List<Employee> list = ...;
Map<Integer,List<Employee>> map = list.stream()
.collect(Collectors.groupingBy(Employee::getDepId));
如果您使用Guava并想使用适当的集合类型,请在这里ListMultimap<Integer, Employee>
,使用Multimaps#index()
:
ListMultimap<Integer, Employee> m = Multimaps.index(list, Employee::getDepId);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.