zl程序教程

您现在的位置是:首页 >  其他

当前栏目

18、实体类对象比对-JSON

2023-04-18 15:18:47 时间

实体类对象相互比较-JSON方式:

在实际开发中,我们经常需要比较同一个自定义类型的两个不同对象的属性值是否相等,采用JSON方式比较可快速有效实现相关需求。JSONobject是FastJson提供的对象,在API中是用一个私有的常量map进行封装的,实际就是一个map,只不过 FastJson对其进行了封装,添加了很多方便快捷的属性方法。

1、相关依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.79</version>
</dependency>

 

2、自定义JSON比较工具方法:

    public static Boolean emtityToCompare(Object oldObject, Object newObject){
        JSONObject jsonObject1 = JSONObject.parseObject(JSON.toJSONString(oldObject));
        log.info("JSON对象1:{}",jsonObject1);
        JSONObject jsonObject2 = JSONObject.parseObject(JSON.toJSONString(newObject));
        log.info("JSON对象2:{}",jsonObject2);
        if(ObjectUtils.isEmpty(jsonObject1) && ObjectUtils.isEmpty(jsonObject2)){
            return true;
        }else {
            if(ObjectUtils.isEmpty(jsonObject1)){
                return false;
            }else {
                return jsonObject1.equals(jsonObject2);
            }
        }
    }

 

3、验证:

(1)、声明实体类:

无重写hashCode()和 equals()方法

public class PropertiesVO{
    private String type;
    private Integer cpu;
    private Integer ram;
    private Integer disk;


    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Integer getCpu() {
        return cpu;
    }

    public void setCpu(Integer cpu) {
        this.cpu = cpu;
    }

    public Integer getRam() {
        return ram;
    }

    public void setRam(Integer ram) {
        this.ram = ram;
    }

    public Integer getDisk() {
        return disk;
    }

    public void setDisk(Integer disk) {
        this.disk = disk;
    }
}
View Code

(2)、test测试:

@Slf4j
public class EntityDemo {
    public static void main(String[] args) {
        PropertiesVO propertiesVO = new PropertiesVO();
        PropertiesVO propertiesVO1 = null;
        PropertiesVO propertiesVO2 = new PropertiesVO();
        propertiesVO2.setType("0");
        PropertiesVO propertiesVO3 = new PropertiesVO();
        propertiesVO3.setType("0");
        PropertiesVO propertiesVO4 = new PropertiesVO();
        propertiesVO4.setType("1");

        System.out.println(propertiesVO2.equals(propertiesVO3));//false,无重写hashCode()和 equals()方法
        System.out.println(propertiesVO1.equals(propertiesVO));//NullPointerException空指针异常
        System.out.println(emtityToCompare(propertiesVO1, propertiesVO));//true
        System.out.println(emtityToCompare(propertiesVO2, propertiesVO3));//true
        System.out.println(emtityToCompare(propertiesVO, propertiesVO2));//false
        System.out.println(emtityToCompare(propertiesVO3, propertiesVO4));//false

    }

    public static Boolean emtityToCompare(Object oldObject, Object newObject){
        JSONObject jsonObject1 = JSONObject.parseObject(JSON.toJSONString(oldObject));
        log.info("JSON对象1:{}",jsonObject1);
        JSONObject jsonObject2 = JSONObject.parseObject(JSON.toJSONString(newObject));
        log.info("JSON对象2:{}",jsonObject2);
        if(ObjectUtils.isEmpty(jsonObject1) && ObjectUtils.isEmpty(jsonObject2)){
            return true;
        }else {
            if(ObjectUtils.isEmpty(jsonObject1)){
                return false;
            }else {
                return jsonObject1.equals(jsonObject2);
            }
        }
    }
}
View Code

 

Java中==与equals()的区别:

1、==是运算符,用于比较两个变量是否相等;

2、equals()是Object类的方法,用于比较两个对象是否相等;

总结:

基本数据类型比较使用==,比较他们的值。默认情况下,当对象使用==比较时,比较的是内存地址,如果需要比较对象的内容,需要重写equal方法。

 

hashCode()和 equals()方法为什么要同时重写:

equals()方法在其内部是调用了"=="运算符,即在不重写equals方法的情况下,equals方法是比较两个对象是否具有相同内存地址。而equals方法的重写涉及到值传递与地址传递问题(即比较值与地址相等的问题)。而hashCode()默认返回的是这个对象的内存地址,要求相等的对象必须拥有相等的hashcode,所以重写了equals()方法必须要同时重写hashcode()。

 

 

 

JSONobject操作参考

Java基础参考