List<User> to Map<String,List<User>>
List<User> to Map<String,List<User>>
|
ebony0319Oct 9, 2022
1
what's the key?
|
|
jorneyrOct 9, 2022
2
users.stream().collect(Collectors.toMap(User::getUsername, u -> u, (o, n) -> o));
|
|
3
users.stream().collect(Collectors.groupingBy(User::getKey);
|
|
AresxueOct 9, 2022
4
users.stream().collect(Collectors.groupingBy(User::getKey);
|
|
KingOfUSAOct 9, 2022
5
2 楼的写法好点,当 key 有重复的时候可以容错
|
|
selcaOct 9, 2022
6
|
|
lazyfighterOct 9, 2022
8
@KingOfUSA bug 了,看清楚楼主的需求, 二楼的返回值是啥?
|
|
KingOfUSAOct 9, 2022
9
@lazyfighter 啊,我错了,,,
|
|
xuanbgOct 9, 2022
10
3 楼正解,就是按 User 对象的某个字段分组,然后以分组字段的值为 key 把对应分组的内容放进 value 。
|