比较map对象之间的工具类,特殊场景,value的string类型相等,认同为相等(附源码)

比较map对象之间的工具类,特殊场景,value的string类型相等,认同为相等(附源码)

问题背景

这次的项目中有个需求,过滤结果集中的相同的数据,但这个数据集里面的对象是JSONObject或者map,它们中的value值可能是字符串类型,或者number类型。我们则认为相等,则需要过滤

例如,这两个我认为是相等的,虽然value的类型不同

HashMap<String, Object> map1 = new HashMap<>();
HashMap<String, Object> map2 = new HashMap<>();
map1.put("a", 1);
map2.put("a", "1");

注意事项:

工具类编写

1 引入一个先验知识,HashMap和JSONObject的equals是重写过的,所以使用equals是进行内容比较,例如

HashMap<String, Object> map1 = new HashMap<>();
HashMap<String, Object> map2 = new HashMap<>();
map1.put("a", "1");
map2.put("a", "1");
System.out.println(map1 == map2); 		 //结果为false
System.out.println(map1.equals(map2));   //结果为true

map1和map2是两个不用的对象,但内容是相等的,所有equals是true,JSONObject也是这样的

2 新建工程,引入pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yg</groupId>
    <artifactId>mapUtils</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mapUtils</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3 新建工具类

package com.yg.maputils.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.*;

/**
 * @Author suolong
 * @Date 2022/5/18 17:04
 * @Version 2.0
 */
public class MapUtils {

    public static boolean mapCompare(Map<String, Object> map1, Map<String, Object> map2) {
        if (map1.size() != map2.size()) {
            return false;
        }
        List<String> keyList1 = new ArrayList<>(map1.keySet());
        List<String> keyList2 = new ArrayList<>(map2.keySet());
        for (String key : keyList1) {
            if (keyList2.contains(key)) {
                String value1 = String.valueOf(map1.get(key));
                String value2 = String.valueOf(map2.get(key));
                if (!value1.equals(value2)) {
                    return false;
                }
            } else {
                return false;
            }
        }
        return true;
    }


    public static List<HashMap<String, Object>> deduplicateMap(List<HashMap<String, Object>> mapList) {
        Set<HashMap<String, Object>> duplicateSet = new HashSet<>(mapList);
        mapList = new ArrayList<>(duplicateSet);
        List<HashMap<String, Object>> duplicateList = new ArrayList<>();
        int size = mapList.size();
        for (int i = 0; i < size; i++) {
            HashMap<String, Object> map1 = mapList.get(i);
            for (int j = i + 1; j < size; j++) {
                HashMap<String, Object> map2 = mapList.get(j);
                if (mapCompare(map1, map2)) {
                    duplicateList.add(map2);
                }
            }
        }
        for (HashMap<String, Object> duplicate : duplicateList) {
            mapList.remove(duplicate);
        }
        return mapList;
    }


    public static List<JSONObject> deduplicateJSONObject(List<JSONObject> jsonObjectList) {
        Set<JSONObject> duplicateSet = new HashSet<>(jsonObjectList);
        jsonObjectList = new ArrayList<>(duplicateSet);
        List<JSONObject> duplicateList = new ArrayList<>();
        int size = jsonObjectList.size();
        for (int i = 0; i < size; i++) {
            JSONObject jsonObject1 = jsonObjectList.get(i);
            for (int j = i + 1; j < size; j++) {
                JSONObject jsonObject2 = jsonObjectList.get(j);
                if (mapCompare(jsonObject1, jsonObject2)) {
                    duplicateList.add(jsonObject2);
                }
            }
        }
        for (JSONObject duplicate : duplicateList) {
            jsonObjectList.remove(duplicate);
        }
        return jsonObjectList;
    }

}

4 主函数测试

package com.yg.maputils.service;

import com.alibaba.fastjson.JSONObject;
import com.yg.maputils.utils.MapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author suolong
 * @Date 2022/5/19 15:21
 * @Version 2.0
 */
public class Main {

    public static void main(String[] args) {
        HashMap<String, Object> map1 = new HashMap<>();
        HashMap<String, Object> map2 = new HashMap<>();
        HashMap<String, Object> map3 = new HashMap<>();
        HashMap<String, Object> map4 = new HashMap<>();
        HashMap<String, Object> map5 = new HashMap<>();
        map1.put("a", 1);
        map2.put("a", "1");
        map3.put("a", "1");
        map4.put("a", "1");
        map5.put("a", "1");
        System.out.println(map1.equals(map2));
        System.out.println(MapUtils.mapCompare(map1, map2));
        List<HashMap<String, Object>> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        System.out.println(list);
        System.out.println(MapUtils.deduplicateMap(list));
        System.out.println();
        System.out.println();
        JSONObject json1 = new JSONObject();
        JSONObject json2 = new JSONObject();
        JSONObject json3 = new JSONObject();
        JSONObject json4 = new JSONObject();
        JSONObject json5 = new JSONObject();
        json1.put("a", 1);
        json2.put("a", "1");
        json3.put("a", "1");
        json4.put("a", "1");
        json5.put("a", "1");
        System.out.println(json1.equals(json2));
        System.out.println(MapUtils.mapCompare(json1, json2));
        List<JSONObject> list1 = new ArrayList<>();
        list1.add(json1);
        list1.add(json2);
        list1.add(json3);
        list1.add(json4);
        list1.add(json5);
        System.out.println(list1);
        System.out.println(MapUtils.deduplicateJSONObject(list1));
    }

}

5 测试结果,过滤之后,只剩下一个map了

总结

  • 可能遇到的场景不多,需要的同学直接获取参考就行




作为程序员第 138 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 在我忘记之前

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值