发布于 4年前

Java8使用Lambda把List转换为Map

使用Java8的Collections可以很简便地把List转换为Map

以User为例

List里的User名字唯一

Map<String, User> result =
    users.stream().collect(Collectors.toMap(User::getName,
                                              Function.identity()));

List里的User存在重名

Map<String, List<User>> result =
 users.stream().collect(Collectors.groupingBy(Choice::getName));
©2020 edoou.com   京ICP备16001874号-3