添加 equals() 和 hashCode() 方法会破坏某些东西吗

Mic*_*u93 2 java equals hashcode

添加equals()hashCode()方法是否有可能破坏已经存在的代码?

我有一个包含 3 个字段、getter 和 setter 的类:

public class Person {
private final String name;
private final List<Friend> friends;
private final Integer age;
Run Code Online (Sandbox Code Playgroud)

为了测试这个类,我使用了:isEqualToComparingFieldByField()方法来比较两个Person对象而不是添加equals()hashCode()。其他解决方案是覆盖equals()hashCode()使用assertEquals()来比较这些对象,但我可以完全确定它不会破坏任何东西吗?

Jon*_*eet 12

我可以完全确定它不会破坏任何东西吗?

不。您正在将相等的含义从引用标识更改为某种值相等。你会破坏任何依赖于当前行为的东西。例如,这里有一些有效的代码:

Person person1 = new Person("Foo", 100);
Person person2 = new Person("Foo", 100);
// This is fine. Two distinct Person objects will never be equal...
if (person1.equals(person2)) {
    launchNuclearMissiles();
}
Run Code Online (Sandbox Code Playgroud)

您提议的更改将打破这一点。

真的有这样的代码吗?很难说。

更有可能的是,如果您想更改hashCode为包含来自 的散列List<Friend>,您很容易破坏代码,除非该类型实际上是不可变的。例如:

Map<Person, String> map = new HashMap<>();
Person person = new Person("Foo", 100);
map.put(person, "Some value");

// If this changes the result of hashCode()...
person.addFriend(new Friend("Bar"));
// ... then you may not be able to find even the same object in the map.
System.out.println(map.get(person));
Run Code Online (Sandbox Code Playgroud)

从根本上说,您需要了解其他代码使用的内容Person,以便了解它所依赖的内容。ifPerson是不可变的,这让生活变得更简单,因为你不需要担心第二类问题。(覆盖equals()hashCode()用于可变类型从根本上来说是一项危险的业务。)