按月份将List进行分组,再排序

1、按月份将List进行分组

  Map<String, List<PrsCalcData>> result = data.stream().collect(Collectors.groupingBy(PrsCalcData::getSmo));

2、注意此处的key是string类型,是用于一般排序

  Map<String,List<PrsCalcData>> smoMap = result.entrySet().stream()
          .sorted(Map.Entry.comparingByKey())
          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                 (oldValue, newValue) -> oldValue, LinkedHashMap::new));

3、注意由于,String类型的"11"比"2"大,所以需要转换一下

   Map<String,List<PrsCalcData>> smoMap = result.entrySet().stream()
          .sorted(new Comparator<Map.Entry<String, List<PrsCalcData>>>() {
            @Override public int compare(Map.Entry<String, List<PrsCalcData>> o1, Map.Entry<String, List<PrsCalcData>> o2) {
              Integer key1 = Integer.parseInt(o1.getKey());
              Integer key2 = Integer.parseInt(o2.getKey());
              return key1.compareTo(key2);
            }
          })
          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                  (oldValue, newValue) -> oldValue, LinkedHashMap::new));
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐