java8利用stream对集合进项分组,合并,排序

//对集合进行筛选后再使用collect()方法把集合转成list
List<EsenResultDayThreeHistogram> list1 = list.stream().filter(e -> e.getGraphType().equals(Constants.GROUP_TYPE_NUM))
                                        .collect(Collectors.toList());
//对list进行分组,求和。
Map<String,Integer> map2  = list1.stream().collect(Collectors.groupingBy(EsenResultDayThreeHistogram::getyType,
                                                    Collectors.summingInt(EsenResultDayThreeHistogram::getsCount)));
//对map里的value进行排序
List<Map.Entry<String,Integer>> entryArrayList = new ArrayList<>();
entryArrayList.addAll(map2.entrySet());
Collections.sort(entryArrayList, (o1, o2) -> o2.getValue()-o1.getValue());

猜你喜欢

转载自blog.csdn.net/qq_39438729/article/details/80841069