Such as different two kind of new properties of the same object, if the property value and hashcode way of rewriting the same way, the same hashcode, what also doesn't explain, at most two objects must be unequal, finally is equals to the and==there is comparative, not rewritten as return before (this==obj); Two effects are the same, but rewritten as follows:
Equals rewritten, first of all determine whether the two reference object reference address, if the same is cited the inside the same object (because of the==comparison real address), the same object, there is no question that must be the same; Otherwise, then run the method, according to the reflection determine whether from the same Class Class, avoids the similar attributes and attribute values same problem, this is the place better than hashcode, such as the above examples of hashcode, if not also showed the same hashcode an object, and then, if the same, as are compared, and the following basic types using the==comparison, in the base type, if the same numerical returns true, it has been done in this paper, the basic types of comparing equals, but ultimately did use the==comparison, without any relationship of hashcode, so I just wondering why rewrite equals to rewrite the hashcode?
Above is my to==, hashcode, equals the thick thin see, have wrong place entreated you pointed out,
CodePudding user response:
Basically:HashCode is mainly used to improve the query efficiency of improving the performance of hash table to determine the object stored in the hash structure address
1, override the equals () must override hashCode ()
2, the hash in the storage structure, add elements repetitive calibration standard is first check hashCode values, after judging the equals ()
Equal 3, two objects the equals () and hashcode () must be equal to
4, two objects hashcode () and equals () must also differ
5, two objects hashcode () are equal, object is not necessarily equal, need through the equals () further judgment,
Rewrite equals after must rewrite hashcode purpose is to ensure that equals the same objects in the hashmap/hashset the same hash bucket, so as to improve the retrieval speed,
CodePudding user response:
I think the original poster is misunderstood hashCode usage, an object's hashCode is to determine the uniqueness of the object, is the perfect solution for each instantiation of the object has a completely unique hash code as the uniqueness of his label, but the "uniqueness" are difficult to achieve, so there was a compromise agreement: if two object instances are the same a.e quals==true that a and b (b) the hash code must be the same, but if a.e quals (b)==false, their hashCode may also be the same,Hash Code used mainly in the Hash by looking for key of the implementation of data structure, the corresponding values, the aim is to search and return the target object instance, one of the most commonly used is a hashmap. Can you imagine a hashmap as a row of barrels, each barrel label represents a Hash value, if according to the most perfect condition, each Hash Code value is unique, then each bucket, there is only one instance will very quickly find, but the reality could be more elements, each bucket when find the barrels, system also need according to the equals method from the multiple elements to find corresponding instance,
Java provides the Comparator/Comparable interface to implementation instance comparison
CodePudding user response:
"Why rewrite equals to rewrite the hashcode"
Because there is an important part of the specification: equals return true two objects, their hashcode must
So if you just override the equals, hashcode probably return value is not the same as
So, in the ArrayList class array type seems to don't see any problems in the collection
But if it is a HashMap key, so the question is exposed in the
This is not just the problem of retrieval speed
But you probably cannot do you expect from a HashMap the element
You look at the HashMap get source:
Public synchronized V get (Object key) {
Entry TAB []=table;
Int hash=key. HashCode ();
Int index=(hash & amp; 0 x7fffffff) % TAB. The length;
For (EntryE=TAB [index]; E!=null; E=e.n ext) {
If ((e.h ash==hash) & amp; & E.k ey. Equals (key)) {
Return e.v alue.
}
}
return null;
}
Focus on this sentence: if ((e.h ash==hash) & amp; & E.k ey. Equals (key)) {
If your object is as HashMap key
Is first to determine hashcode
HashSet also have the same problem, first determine hashcode
If the hashcode is different, have already considered the different objects,
Is your equals two object to true if you don't like hashcode, exists in the Set will repeat, contrary to the meaning of the Set