将List 转为Map 并按照对象中的指定属性进行 分组 和 去重
2020-08-11 19:02
46 查看
这是 公共方法 (摘抄)
传值:List list (需要转化的数据) 和 map(这个map是空的 用来接收转化的map)
返回值:转化好的map
public static Map<String, List<UintCardreAlertVO>> fenZu(List<UintCardreAlertVO> list, Map<String, List<UintCardreAlertVO>> map) {//map是用来接收分好的组的 if (null == list || null == map) { return null; } String key; List<UintCardreAlertVO> listTmp; for (UintCardreAlertVO val : list) { key = val.getB0111Result();//按这个属性分组,map的Key listTmp = map.get(key); if (null == listTmp) { listTmp = new ArrayList<UintCardreAlertVO>(); map.put(key, listTmp); } listTmp.add(val); } return map; }
调用上面的方法进行转换 ,按对象中指定的属性分好组 后 在去重
//创建一个用来接收的map Map<String, List<UintCardreAlertVO>> resultMap = new HashMap<>(); Map<String, List<UintCardreAlertVO>> stringListMap = fenZu(alertAllList, resultMap);//调用上面的方法进行分组 alertAllList是传进的list,resultMap是传进去用来接收数据的空map // 将处理好分组的map 遍历 List<UintCardreAlertVO> newList = new ArrayList<>(); for(Map.Entry<String, List<UintCardreAlertVO>> entry:stringListMap.entrySet()){ List<UintCardreAlertVO> valueList = entry.getValue(); // 将 list中对象中的 A0000 重复的进行去重 newList.addAll(valueList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getA0000()))), ArrayList::new))) ;// 里面这个lambad表达式 就是按照对象的属性进行去重的 :参数1:valueList 你要去重的List<UintCardreAlertVO>(对象集合) 参数2 :getA0000 按照对象中的这个属性去重 }
相关文章推荐
- java list按照 对象 指定多个字段属性进行排序
- java list按照元素对象的指定多个字段属性进行排序
- java list按照元素对象的指定多个字段属性进行排序
- java list按照 对象 指定多个字段属性进行排序
- java list按照 对象 指定多个字段属性进行排序
- 将List(map(String,Object))对象按照Map的某个key的值进行分组并降序排列
- java list按照元素对象的指定多个字段属性进行排序
- java list按照元素对象的指定多个字段属性进行排序
- java list按照元素对象的指定多个字段属性进行排序
- java list按照元素对象的指定多个字段属性进行排序
- java list按照元素对象的指定多个字段属性进行排序
- java list按照元素对象的指定多个字段属性进行排序
- 按照List中Object的某个属性进行分组
- 对list集合中的对象按照对象的某一属性进行排序
- Python 列表中存储类对象时按照指定属性进行排序——用于正排表的排序
- LinkedList中将对象按照某一属性排序,利用 collection.sort(); 来进行调用!
- C# List按照对象属性进行排序
- Java list里面分装的都是对象,按照对象的属性来分组
- java list分组 list里面分装的都是对象 按照对象的属性来分组
- Java 中List数据,自定义对象按指定的属性进行排序