First refer to the article: Http://www.oschina.net/translate/working-with-hashcode-and-equals-methods-in-java
What is the difference between the 1,equals method and = =? Why do I need to rewrite the Equals method?
2, why is it best to rewrite the Equals method by overriding the Hashcode method? There is an example in the reference post explaining the reason for this problem.
3, how do I override the Equals method and the Hashcode method?
————————————————————————————————————————————————
The 1,equals method is in the object class, and any class inherits from the object class, so any class has the Equals method. The principle of the Equals method in the object class to compare two objects is whether two objects are in the same position in memory (as with = =) and are not compared based on the characteristics of the object, so this is why you need to override the Equals method.
For example: The person class, in real life, as long as two ID card number is the same, it is considered to represent the same person. So, no matter how many person objects you new, as long as the ID (ID) attribute of these person objects is the same, Object 1.equals (object 2) should return true, regardless of the position of object 1, Object 2 in memory!
2, overriding equals Why rewrite hashcode?
The reference blog post at the beginning of this article has already mentioned a reason. That is, since object 1.equals (object 2) returns true that the two objects should actually be the same object, they should be stored in the same location in memory. And Hashcode is the physical intrinsic address used to calculate the placement of objects.
For hashcode effect inheritance refer to the following article:
http://c610367182.iteye.com/blog/1930676
http://blog.csdn.net/fenglibing/article/details/8905007
3, this should be based on the actual need to rewrite. For the Equals method, there is a fixed pattern, (this = = obj obj==null ...). This does not introduce. Note that you can use the Generate equals and Hashcode methods in eclipse. (Source-->generate hashcode () and equals ...)
The Equals method and Hashcode method learning in Java