How to evaluate a list in the Java equation of two objects

I have an ArrayList. I want to use the contains (thing o) method to check the equality of the two objects. I override the equals () method in the thing class, but it doesn't work when I call the contains method!

Is it necessary to override the hashcode () method? If so, how do I overwrite it?

Solution

You should override hashcode The ArrayList class does not use the hashcode method, so it is not needed now. However, if you use HashMap, HashSet or any other collection using hashcode at any time, the program will be interrupted because of hashcode and equals are not consistent

In this case, the simple implementation of hashcode can be:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>