//使用Stream流对一个集合按照年龄进行分组
@Test
public void streamDemo05(){
List<Teacher> teacherList = new ArrayList<>();
teacherList.add(new Teacher(1L, "张三", 20));
teacherList.add(new Teacher(2L, "张三", 23));
teacherList.add(new Teacher(3L, "李四", 21));
teacherList.add(new Teacher(4L, "王五", 21));
teacherList.add(new Teacher(5L, "赵六", 24));
//按照年龄分组
Map<Integer, List<Teacher>> groupTeacherMap = teacherList.stream().collect(Collectors.groupingBy(Teacher::getAge));
System.out.println(groupTeacherMap.get(21));
/**结果:
* [Teacher(id=3, name=李四, age=21), Teacher(id=4, name=王五, age=21)]
*/
}
01-03
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-06
1537
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-18
1168
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-25
4266
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-25