您的位置:首页 > 其它

将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 按照对象中的这个属性去重

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: