List<User> to Map<String,List<User>>
1
ebony0319 2022-10-09 13:40:07 +08:00
what's the key?
|
2
jorneyr 2022-10-09 13:42:27 +08:00
users.stream().collect(Collectors.toMap(User::getUsername, u -> u, (o, n) -> o));
|
3
anakinsky 2022-10-09 13:48:45 +08:00 5
users.stream().collect(Collectors.groupingBy(User::getKey);
|
4
Aresxue 2022-10-09 14:11:22 +08:00
users.stream().collect(Collectors.groupingBy(User::getKey);
|
5
KingOfUSA 2022-10-09 16:36:48 +08:00
2 楼的写法好点,当 key 有重复的时候可以容错
|
6
selca 2022-10-09 16:42:15 +08:00
|
8
lazyfighter 2022-10-09 16:43:13 +08:00
@KingOfUSA bug 了,看清楚楼主的需求, 二楼的返回值是啥?
|
9
KingOfUSA 2022-10-09 16:46:50 +08:00
@lazyfighter 啊,我错了,,,
|
10
xuanbg 2022-10-09 16:47:55 +08:00
3 楼正解,就是按 User 对象的某个字段分组,然后以分组字段的值为 key 把对应分组的内容放进 value 。
|