java8 按两个属性分组,并返回扁平List; stream排序
---------------
java8 按两个属性分组,并返回扁平List
/** * 设置大区小区分组排序 * @param dtoList */ private List<PerformanceDto> getRegionGroupOrderList(List<PerformanceDto> dtoList) { return dtoList.stream() .collect(Collectors.groupingBy(PerformanceDto::getLargeRegion, Collectors.groupingBy(PerformanceDto::getSmallRegion))) .entrySet().stream() .flatMap(p -> p.getValue().entrySet().stream().flatMap(a -> a.getValue().stream())) .collect(Collectors.toList()); }
排序:
List<Student> studentList1=studentList.stream().sorted().collect(Collectors.toList());//自然序列 List<Student> studentList2=studentList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());//逆序 List<Student> studentList3=studentList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());//根据年龄自然顺序 List<Student> studentList4=studentList.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());//根据年龄逆序