首页 > 解决方案 > 为什么 Equals 和 Hashcode 对于对象列表是相同的,即使我没有为该对象实现这些方法

问题描述

我有 3 节课

员工类就像

  class Employee {
    int id;
    String name;
    long salary;
    List<Address> address;
    //getter setter, equals and hashcode and parameterized constructor}

我的地址类就像

public class Address {
int housenum;
String streetname;
int pincode;
//getter setter and parameterized constructor
}

我的测试课就像

 Address address1 = new Address(10, "str 1", 400043);
            Address address2 = new Address(10, "str 1", 400043);

            List<Address> addressLst1= new ArrayList<>();
            List<Address> addressLst2= new ArrayList<>();
            addressLst1.add(address1);
            addressLst1.add(address2);
            addressLst2.add(address1);
            addressLst2.add(address2);
            Employee employee1 = new Employee(1, "EMP1", 1000, addressLst1);
            Employee employee2 = new Employee(1, "EMP1", 1000, addressLst2);

            Set<Employee> set = new HashSet<>();
            set.add(employee1);
            set.add(employee2);

            System.out.println(":::::::::::::::addressLst1:::::::::" + addressLst1.hashCode());
            System.out.println(":::::::::::::::addressLst2:::::::::" + addressLst2.hashCode());

            System.out.println(":::::::::::::::address1:::::::::" + address1.hashCode());
            System.out.println(":::::::::::::::address2:::::::::" + address2.hashCode());

            System.out.println(":::::::::::::::employee1:::::::::" + employee1.hashCode());
            System.out.println(":::::::::::::::employee2:::::::::" + employee2.hashCode());

            set.forEach(System.out::println);

            System.out.println(":::::::::::::::size:::::::::" + set.size());

因为我没有覆盖等于和哈希码,所以我得到了地址对象的不同哈希码。但是为什么我会为两个不同的地址列表(即 addressLst1 和 addressLst2)获得相同的哈希码以及为什么我将设置的大小设置为 1,为什么两个员工对象的哈希码相同?对于由另一个自定义对象列表组成的自定义对象,覆盖等于和哈希码的正确方法是什么?

标签: javacollectionshashmapequalshashcode

解决方案


这两个ListsaddressLst1addressLst2包含完全相同的元素以完全相同的顺序,因此Lists的合同equals要求这两个Lists 将彼此相等:

boolean java.util.List.equals(Object o)

比较指定对象与此列表是否相等。当且仅当指定对象也是一个列表时返回 true,两个列表具有相同的大小,并且两个列表中所有对应的元素对都相等。(如果 (e1==null ? e2==null :e1.equals(e2)) 两个元素 e1 和 e2 相等。)换句话说,如果两个列表以相同的顺序包含相同的元素,则它们被定义为相等. 此定义确保 equals 方法在 List 接口的不同实现中正常工作。

Address不覆盖equalsand并不重要hashCode,因为Lists 包含对相同对象的引用。虽然address1不等于address2,但两个Lists 都包含对两者的引用address1address2顺序相同,因此Lists 相等。

对于Employee该类,您写道您确实覆盖了equalsand hashCode,所以我假设Employees如果它们的所有属性都相等,则它们是相等的。因此,Employee您尝试添加到的两个实例Set是相等的。


推荐阅读