Collectors.groupingBy 用法记录
public class test {
public static void main(String[] args) throws ParseException {
// groupingBy
Map<String, List
for (String s : tempMap.keySet()) {
System.out.println(s);
tempMap.get(s).stream().forEach(x -> System.out.println(x));
}
Map<String, List<Person>> tempMap1 = Stream.of(new Person("1", "aa", "13"), new Person("1", "aa", "11"), new Person("1", "aa", "12"))
.collect(Collectors.groupingBy(x -> fetchGroupKey(x)));
System.out.println(tempMap1.keySet());
for (String s : tempMap1.keySet()) {
System.out.println(s);
tempMap1.get(s).stream().forEach(x -> System.out.println(x));
}
}
private static String fetchGroupKey(Person person){
return person.getId()+"#"+person.getName()+"#"+person.getAge();
}
}
Map<String, List
Map<String, List
以上代码输出:
1
Person{id='1', name='aa', age='12'}
Person{id='1', name='bb', age='13'}
3
Person{id='3', name='cc', age='14'}
[1#aa#12, 1#aa#13, 1#aa#11]
1#aa#12
Person{id='1', name='aa', age='12'}
1#aa#13
Person{id='1', name='aa', age='13'}
1#aa#11
Person{id='1', name='aa', age='11'}