Study notes for Hash Code and Equals
I. Definition
1) What is hash Code?
Hash code is a way of encoding, in Java, each object will have a Hashcode,java can use this hashcode to identify an object. As for the specific coding of hashcode, it is more complicated (in fact, the code can be rewritten by the programmer through inheritance and interface implementation).
in the simplest way, hashcode is a signature. When the hashcode of two objects is the same, two objects are likely to be the same. If it's not the same, two objects are definitely different. generally use hashcode to compare two things are not the same, can easily exclude a lot of different things. the most common place is to find something in a pile of things. Compare the hashcode of what you are looking for with the hashcode of everything, if not the same, it is certainly not what you are looking for. If the same thing is probably what you are looking for. Then make a careful comparison of two things is not really exactly the same.
2) What is equals?
the Equals () method is used to compare a string to a specified object.
This method first compares whether the address of two objects is the same, and if the same returns true directly, otherwise,
(1) If the string type is the first comparison is a string type, yes, and then compare whether the same length, the same words and then compare, each character is the same;
(2) Determine whether two objects are loaded with the same classloader, and not return false;
(3) If it is a normal object, put the object's attributes into HashMap as the key, and the value in the attribute as value, as the way to compare.
Two. Feature:
1) Hash Code
On the Hashcode method, the consistent convention is:
An object that overrides the Euqls method must override the Hashcode () method at the same time.
If 2 objects returned by equals are true, then the Hashcode method of the 2 object must return the same int type hash code
If 2 objects return false through Equals, their hashcode return value allows the same. (However, programmers must be aware that Hashcode returns a unique hash code that will allow the hashtables of the object to be stored to work better.)
2) Equals
By convention, equals satisfies the following rules.
Reflexivity: X.equals (x) must be True
Must be false for null:x.equals (NULL)
Symmetry: X.equals (y) and y.equals (x) result consistent
Transitivity: A and B equals, B and C equals, then A and C must also be equals.
Consistency: During a runtime, changes in the state of 2 objects do not affect the decision result of equals, so the same result is returned during this runtime, regardless of how many times equals is called.
Three. Relationship
1. The Equals method is used to compare the contents of the object (after overwriting);
2. The Hashcode method is used only in the collection;
3. When the Equals method is overridden, the comparison objects are compared by the overridden Equals method (judging whether the object's contents are equal);
4. When putting an object into a collection, first determine whether the Hashcode value to be placed on the object is equal to the hashcode value of any of the elements in the collection, and if not equal, put the object directly into the collection. If the hashcode value is equal, then the Equals method is used to determine if any of the objects in the collection are equal, and if equals does not determine equality, the element is placed directly into the collection, otherwise it is not put.
Excerpt from: https://www.cnblogs.com/lulipro/p/5628750.html
Https://www.cnblogs.com/wangnuo/p/7744891.html
Https://www.cnblogs.com/keyi/p/7119825.html
Java EE Week Eighth