Java中如何判断两个对象是否相等(How to judge whether two objects are equal in Java)

参考:https://blog.csdn.net/u013063153/article/details/78808923

下面是自己开发过程中的实现

package com.***.***.entityimport lombok.Data;import lombok.ToString;
@ToString(callSuper = true)
@Data
public class ****Scene implements Scene{
    String method;
    String chunk;
    String size;
    int concurrent;
    /**
    *重写了equals方法,两个对象相等要么两个对象的地址相同,要么两个对象的内容相同
    */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ****Scene other = (****Scene) obj;
        if (concurrent != other.concurrent)
            return false;
        if (chunk == null) {
            if (other.chunk != null) {
                return false;
            }
        } else if (!chunk.equals(other.chunk))
            return false;
        if (size == null) {
            if (other.size != null) {
                return false;
            }
        } else if (!size.equals(other.size))
            return false;
        if (method == null) {
            if (other.method != null) {
                return false;
            }
        } else if (!method.equals(other.method))
            return false;
        return true;
    }
    public String getString(){
        return method+"_"+size+"_"+chunk+"_";
    }
}
————————

参考:https://blog.csdn.net/u013063153/article/details/78808923

The following is the implementation in your own development process

package com.***.***.entityimport lombok.Data;import lombok.ToString;
@ToString(callSuper = true)
@Data
public class ****Scene implements Scene{
    String method;
    String chunk;
    String size;
    int concurrent;
    /**
    *重写了equals方法,两个对象相等要么两个对象的地址相同,要么两个对象的内容相同
    */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ****Scene other = (****Scene) obj;
        if (concurrent != other.concurrent)
            return false;
        if (chunk == null) {
            if (other.chunk != null) {
                return false;
            }
        } else if (!chunk.equals(other.chunk))
            return false;
        if (size == null) {
            if (other.size != null) {
                return false;
            }
        } else if (!size.equals(other.size))
            return false;
        if (method == null) {
            if (other.method != null) {
                return false;
            }
        } else if (!method.equals(other.method))
            return false;
        return true;
    }
    public String getString(){
        return method+"_"+size+"_"+chunk+"_";
    }
}