java - 为什么 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,为什么两个员工对象的哈希码相同?对于由另一个自定义对象列表组成的自定义对象,覆盖等于和哈希码的正确方法是什么?
解决方案
这两个List
saddressLst1
和addressLst2
包含完全相同的元素以完全相同的顺序,因此List
s的合同equals
要求这两个List
s 将彼此相等:
boolean java.util.List.equals(Object o)
比较指定对象与此列表是否相等。当且仅当指定对象也是一个列表时返回 true,两个列表具有相同的大小,并且两个列表中所有对应的元素对都相等。(如果 (e1==null ? e2==null :e1.equals(e2)) 两个元素 e1 和 e2 相等。)换句话说,如果两个列表以相同的顺序包含相同的元素,则它们被定义为相等. 此定义确保 equals 方法在 List 接口的不同实现中正常工作。
Address
不覆盖equals
and并不重要hashCode
,因为List
s 包含对相同对象的引用。虽然address1
不等于address2
,但两个List
s 都包含对两者的引用address1
且address2
顺序相同,因此List
s 相等。
对于Employee
该类,您写道您确实覆盖了equals
and hashCode
,所以我假设Employees
如果它们的所有属性都相等,则它们是相等的。因此,Employee
您尝试添加到的两个实例Set
是相等的。
推荐阅读
- c# - 随机数地点到财产
- laravel - 我的 Post 请求将转到生产服务器上的 Page Expired 页面
- excel - 在 Excel 中更新主工作表上的值
- entity-framework - 在 docker 内运行的 .net 3.1 应用程序无法连接 SQL 服务器
- c# - 文件不上传Windows表单C#
- node.js - $ 或聚合在 nodejs 中不工作但在 mongoshell 中工作
- javascript - 从 jquery 中的字符串调用函数,但传递的参数未正确显示
- c++ - 具有传递依赖的 findFoo.cmake
- c++ - 这是 GoogleTest AssertionResult 中的错误吗?
- file-upload - swagger-annotations @Schema 不提供定义属性的方法