List<Map> 合并去重

去重之前,将有值的对象赋给没值的对象,再进行合并

    // 去重合并
    public static List<Map<String, Object>> filterListByName(List<Map<String, Object>> list) {
        List<Map<String, Object>> list1 = new ArrayList<>();
        if (null == list || list.size() <= 0) {
            return list;
        } else {
            // 填充空值
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map1 = list.get(i);
                Map<String, Object> mapbeforeAll = new HashMap<>();
                for (int j = i + 1; j < list.size(); j++) {
                    Map<String, Object> map2 = list.get(j);
                    // 获取前后更改后的相同key值
                    for (String key1 : map1.keySet()) {
                        if (!Objects.isNull(map1.get(key1))) {
                            mapbeforeAll.put(key1, map1.get(key1));
                        } else {
                            mapbeforeAll.put(key1, map2.get(key1));
                        }
                    }
                    list1.add(mapbeforeAll);
                }
            }
            // 去除重复元素
            for (int i = 0; i < list1.size(); i++) {
                Map<String, Object> map1 = list1.get(i);
                for (int j = i + 1; j < list1.size(); j++) {
                    if (map1.get("villagecode").equals(list1.get(j).get("villagecode"))) {
                        list1.remove(j);
                        j--;
                    }
                }
            }
            return list1;
        }
    }

你可能感兴趣的:(java,散列表)