List<Student> ls=new ArrayList<>();
Student s1 = new Student("小龙女", 23);
Student s2 = new Student("任盈盈", 24);
//Student s3 = new Student("小龙女", 23);
Student s4 = new Student("东方不败", 25);
Student s5 = new Student("伊琳", 29);
Student s6 = new Student("周芷若", 30);
ls.add(s1);
ls.add(s2);
ls.add(s4);
ls.add(s5);
ls.add(s6);
Map<String, List<Student>> collect = ls.stream().collect(Collectors.groupingBy(Student::getName));
System.out.println(collect);
Set<Map.Entry<String, List<Student>>> entries = collect.entrySet();
System.out.println(entries);
Iterator<Map.Entry<String, List<Student>>> iterator1 = entries.iterator();
// while (iterator1.hasNext()){
// List<Student> value = iterator1.next().getValue();
// System.out.println(value.toString());
// }
//利用的Map.Entry
for(Map.Entry<String, List<Student>> en : collect.entrySet()){
System.out.println(en.getValue());
System.out.println(en.getKey());
}
Map集合的entrySet和keySet
最新推荐文章于 2021-04-21 15:56:08 发布