java - 为什么对象列表的 Equals 和 Hashcode 是相同的,即使我没有为该对象实现这些方法

标签 java collections hashmap equals hashcode

我有3节课

Employee类就像

  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());

我得到了地址对象的不同哈希码,因为我没有覆盖 equals 和 hashcode。但是为什么我得到两个不同地址列表(即addressLst1和addressLst2)相同的哈希码,为什么我得到的集合大小为1,为什么两个员工对象的哈希码相同?对于由另一个自定义对象的列表组成的自定义对象,重写 equals 和 hashcode 的正确方法是什么?

最佳答案

两个 ListaddressLst1addressLst2 以完全相同的顺序包含完全相同的元素,因此 List 的约定equals 要求这两个 List 彼此相等:

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

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null :e1.equals(e2)).) In other words, 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.

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

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

关于java - 为什么对象列表的 Equals 和 Hashcode 是相同的,即使我没有为该对象实现这些方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60539303/

相关文章:

java - Java 中的嵌套枚举?

java - 提供 equals() 的替代方案吗?

java - 为什么我们可以将 null 元素添加到 java LinkedList?

java - JmsTemplate 没有发送超时

c# - .NET 反射 - 从反射集合中获取第一项而不强制转换为特定集合

json - 关于 JSON 的哈希到底是什么?

java - 在迭代 map 时,对于一个值,它会被打印,尽管它已经被删除

oracle - 将 SQL 类型转换为 PL SQL 集合/将一种集合类型转换为另一种

Java HashMap、hashCode() equals()——如何实现多个key一致?

java - 将 HashMap 值传递给 url