赞
踩
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer userId;
private String name;
private String email;
}
public class Main {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "email1@demo.com"));
users.add(new User(2, "user2", "email2@demo.com"));
users.add(new User(3, "user3", "email3@demo.com"));
users.add(new User(4, "user4", "email4@demo.com"));
Map<Integer, String> userIdAndName = users.stream().collect(Collectors.toMap(User::getUserId, User::getName));
System.out.println(userIdAndName);
}
}
输出:
{1=user1, 2=user2, 3=user3, 4=user4}
public class Main {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "email1@demo.com"));
users.add(new User(2, "user2", "email2@demo.com"));
users.add(new User(3, "user3", "email3@demo.com"));
users.add(new User(4, "user4", "email4@demo.com"));
Map<Integer, User> userIdAndName = users.stream().collect(Collectors.toMap(User::getUserId, user->user));
System.out.println(userIdAndName);
}
}
输出:
{
1=User(userId=1, name=user1, email=email1@demo.com),
2=User(userId=2, name=user2, email=email2@demo.com),
3=User(userId=3, name=user3, email=email3@demo.com),
4=User(userId=4, name=user4, email=email4@demo.com)
}
如果出现相同的key,那么会抛出重复key的异常,比如我们将userId为3的修改为2,则会和第二个用户冲突:
public class Main {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "email1@demo.com"));
users.add(new User(2, "user2", "email2@demo.com"));
users.add(new User(2, "user3", "email3@demo.com"));
users.add(new User(4, "user4", "email4@demo.com"));
Map<Integer, User> userIdAndName = users.stream().collect(Collectors.toMap(User::getUserId, user->user));
System.out.println(userIdAndName);
}
}
抛出异常:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key User(userId=2, name=user2, email=email2@demo.com)
at java.util.stream.Collectors.lambda$throwingMerger$113(Collectors.java:133)
at java.util.stream.Collectors$$Lambda$3/780237624.apply(Unknown Source)
at java.util.HashMap.merge(HashMap.java:1245)
at java.util.stream.Collectors.lambda$toMap$171(Collectors.java:1320)
at java.util.stream.Collectors$$Lambda$5/1560911714.accept(Unknown Source)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.imooc.uua.rest.Main.main(Main.java:16)
Collectors.toMap支持第三个参数,对重复值进行选取:
public class Main {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "email1@demo.com"));
users.add(new User(2, "user2", "email2@demo.com"));
users.add(new User(2, "user3", "email3@demo.com"));
users.add(new User(4, "user4", "email4@demo.com"));
Map<Integer, User> userIdAndName = users.stream().collect(Collectors.toMap(
User::getUserId,
user->user,
(oldValue,newValue)->newValue));
System.out.println(userIdAndName);
}
}
输出:
{
1=User(userId=1, name=user1, email=email1@demo.com),
2=User(userId=2, name=user3, email=email3@demo.com),
4=User(userId=4, name=user4, email=email4@demo.com)
}
可以看到,只会出现 user3,而 user2 被覆盖掉了
刚才上面出现重复的ID,是根据值进行覆盖,在某些情况下需要映射成列表。即:List -> Map<Integer, List>的情况。
public class Main {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "email1@demo.com"));
users.add(new User(2, "user2", "email2@demo.com"));
users.add(new User(2, "user3", "email3@demo.com"));
users.add(new User(4, "user4", "email4@demo.com"));
Map<Integer, List<User>> userIdAndName = users.stream().collect(Collectors.groupingBy(User::getUserId));
System.out.println(userIdAndName);
}
}
输出:
{
1=[User(userId=1, name=user1, email=email1@demo.com)],
2=[User(userId=2, name=user2, email=email2@demo.com),
User(userId=2, name=user3, email=email3@demo.com)],
4=[User(userId=4, name=user4, email=email4@demo.com)]
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。