Java 1.7 Override of hashCode() not behaving as I would expect

两盒软妹~` 提交于 2019-12-01 19:34:49
dasblinkenlight

The problem is that you have provided a wrong equals: it should be equals(Object), not equals(Car).

Essentially, you have provided an overload instead of an override, so HashMap keeps calling the equals from the base class.

Fixing this problem is simple: add an override that does the cast, and calls the equals method that you wrote, like this:

@Override
public boolean equals(Object other) {
    return (other instanceof Car) && equals((Car)other);
}

Note the use of @Override annotation. It helps Java help you spot issues like this automatically.

Note: with this problem out of the way, consider implementing your hashCode method in a more "frugal" way. Rather than creating a throw-away (this.getCarName() + this.getCarModel()) string simply for the purpose of obtaining its hash code, consider rewriting the method as follows:

public int hashCode() {
    return 31*getCarName().hashCode() + getCarModel().hashCode();
}

or in Java 1.7+ you could write

public int hashCode() { // Thanks, fge, for a nice improvement!
    return Objects.hash(carName, carModel);
}

The problem is not with .hashCode(); the problem is that you don't override .equals()!

Look at your prototype:

public boolean equals(Car car)

Now have a look at the documentation for Object...

What you should override is:

public boolean equals(Object obj)

Hence the error. You did implement hashCode correctly, however you use Object's .equals() implementation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!