ListMap去重,差集(针对集合里面只有Map非实体对象)
问题:由于业务需求,接口的入参出参都是List
public static List deleteDuplicatedMapFromListByKeys(
List<Map<String,Object>> originMapList, List keys){
if(CollectionUtils.isEmpty(originMapList)) return null;
Map tempMap = new HashMap();
for (Map originMap : originMapList) {
String objHashCode = "";
for (Object key : keys) {
String value = originMap.get(key) != null ? originMap.get(key).toString() : "";
objHashCode += value.hashCode();
}
tempMap.put(objHashCode,originMap);
}
List valueList = new ArrayList<>(tempMap.values());
return valueList;
}
测试:
public static void main(String[] args) {
List<Map> list = new ArrayList<>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("id", "11");
map1.put("name", "张翰");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("id", "11");
map2.put("name", "霍尊");
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("id", "22");
map3.put("name", "吴签");
list.add(map1);
list.add(map2);
list.add(map3);
List<Object> deleteDuplicatedKeys = new ArrayList<>();
deleteDuplicatedKeys.add("id");
System.out.println("未去重的集合:"+list);
List list1 = deleteDuplicatedMapFromListByKeys(list, deleteDuplicatedKeys);
System.out.println("去重后的集合:"+list1);
}
测试结果:
未去重的集合:[{name=张翰, id=11}, {name=霍尊, id=11}, {name=吴签, id=22}]
去重后的集合:[{name=霍尊, id=11}, {name=吴签, id=22}]
问题:由于业务需求,接口的入参出参都是List
public static List<Map> differenceMapByList(List<Map> originMapList,List<Map> newMapList){
if(CollectionUtils.isEmpty(originMapList)) return null;
if (CollectionUtils.isEmpty(newMapList)) return null;
List<Map> retList = new ArrayList<>();
List<Map> listAll = new ArrayList<>();
listAll.addAll(originMapList);
listAll.addAll(newMapList);
List<Map> listSameTemp = new ArrayList<>();
originMapList.stream().forEach(a -> {
if(newMapList.contains(a))
listSameTemp.add(a);
});
retList = listSameTemp.stream().distinct().collect(Collectors.toList());
listAll.removeAll(retList);
retList = listAll;
return retList;
}
测试:
public static void main(String[] args) {
List<Map> listMap = new ArrayList<>();
List<Map> list = new ArrayList<>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("id", "11");
map1.put("name", "张翰");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("id", "11");
map2.put("name", "霍尊");
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("id", "22");
map3.put("name", "吴签");
list.add(map1);
list.add(map2);
list.add(map3);
System.out.println("集合1:"+list);
Map<String, Object> map4= new HashMap<String, Object>();
map4.put("id", "22");
map4.put("name", "吴签");
listMap.add(map1);
listMap.add(map4);
System.out.println("集合2:"+listMap);
List list2 = deleteDuplicatedMapFromListByKeys1(list, listMap);
System.out.println("集合的差集:"+list2);
}
结果:
集合1:[{name=张翰, id=11}, {name=霍尊, id=11}, {name=吴签, id=22}]
集合2:[{name=张翰, id=11}, {name=吴签, id=22}]
集合的差集:[{name=霍尊, id=11}]
如果是对象的集合,利用Lambda表达式的方式是很快的哈,但listMap好像不太好用,可能我了解的不深哈,不知道怎么用,只能参考出这种。
参考文章: java 两个List集合各种情况对比处理 Java : ListMap中 根据map的某个key去重
|