简体   繁体   English

如何在Java中比较在属性中也有列表的两个对象

[英]How to compare two Objects in Java that also have lists in its properties

public class Employee{

int empId;
String empName;
List <EmployeeAddress> empAdd;

// getters & setters

}

public class EmployeeAddress{
String city;
String state;

// getters & setters

}

now I make two objects of employee class, and want to compare them. 现在,我制作了两个员工类的对象,并希望将它们进行比较。 I have used list in the employee class since employee can have multiple addresses. 由于员工可以有多个地址,因此我在员工类中使用了列表。 Need help on this 在这方面需要帮助

Because is not clear what you mean by saying 'compare', I suppose you need to override equals method in your Employee class. 因为不清楚您所说的“比较”是什么意思,所以我想您需要在Employee类中重写equals方法。

First of all I suggest you to take a look to this interesting question on SO: What issues should be considered when overriding equals and hashCode in Java? 首先,我建议您看一下SO上的一个有趣问题: 在Java中重写equals和hashCode时应考虑哪些问题?

Then, according to equals method contract for List interface: 然后,按照equals接口List方法合同:

two lists are defined to be equal if they contain the same elements in the same order. 如果两个列表包含相同顺序的相同元素,则两个列表定义为相等。 This definition ensures that the equals method works properly across different implementations of the List interface. 此定义确保equals方法可在List接口的不同实现中正常工作。

So you could code something like: 因此,您可以编写类似以下内容的代码:

@Override
public boolean equals(Object obj) {
    Employee e = (Employee) obj;
    return this.empId == e.empId && this.empName.equals(e.empName) && this.empAdd.equals(e.empAdd);
}

Or you can define your custom logic for list comparison... 或者,您可以定义自定义逻辑以进行列表比较...

First, I would suggest that it would be good to change your List<EmployeeAddress> to Set<EmployeeAddress> . 首先,我建议将List<EmployeeAddress>更改为Set<EmployeeAddress>会很好。 This helps in two ways: 这有两种帮助:

  1. It will avoid duplicate addresses in the list of addresses for each employee. 它将避免在每个员工的地址列表中重复地址。
  2. It will make two employees equal without regard of the order in which addresses appear, since their is no order in a set. 这将使两个雇员相等,而不必考虑地址出现的顺序,因为它们在一组中不是顺序。

Having said that, be sure that the equals method of EmployeeAddress is well implemented too, since the Set interface will require it to function properly when detecting duplicates. 话虽如此,但是请确保EmployeeAddress的equals方法也得到了很好的实现,因为Set接口在检测重复项时将要求其正常运行。 And then you would implement equals as: 然后,您将实现equals为:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof Employee)) {
        return false;
    }
    Employee other = (Employee) obj;
    if (this.empAdd == null) {
        if (other.empAdd != null) {
            return false;
        }
    } else if (!this.empAdd.equals(other.empAdd)) {
        return false;
    }
    if (this.empId != other.empId) {
        return false;
    }
    if (this.empName == null) {
        if (other.empName != null) {
            return false;
        }
    } else if (!this.empName.equals(other.empName)) {
        return false;
    }
    return true;
}

The implementation of the equals method for the EmployeeAddress class should be as follows: EmployeeAddress类的equals方法的实现应如下所示:

    @Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof EmployeeAddress)) {
        return false;
    }
    EmployeeAddress other = (EmployeeAddress) obj;
    if (this.city == null) {
        if (other.city != null) {
            return false;
        }
    } else if (!this.city.equals(other.city)) {
        return false;
    }
    if (this.state == null) {
        if (other.state != null) {
            return false;
        }
    } else if (!this.state.equals(other.state)) {
        return false;
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM