List集合可以通过stream()进行分组,但是一般默认得到的是无需的map集合,那么如何分组成有序的LinkedHashMap集合呢,我们来看下实现方式。
1、首先看下默认的分组,无序的map
Map<String, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getSchool));
这样返回的是无序的map,如果想转有序,只能二次手工转了,非常不方便。
2、直接转有序LinkedHashMap方式
Map<String, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getSchool, LinkedHashMap::new, Collectors.toList()));
用以上的groupingBy指定构造LinkedHashMap即可轻松实现有序。

程序员导航
优网导航旗下整合全网优质开发资源,一站式IT编程学习与工具大全网站
以上就是List stream分组成有序的LinkedHashMap实现方式。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...



