In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail why to rewrite hashCode and equals methods, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
What do hashCode and equals look like if they are not rewritten (native)?
The hashCode value that is not overridden (native) is a value converted based on the memory address.
The equals method, which is not overridden (native), is a way to strictly determine whether an object is equal (object1 = = object2).
/ JAVA Code: public static void main (String [] args) {Object obj1 = new Object (); Object obj2 = new Object (); Object obj3 = obj2; System.out.println ("obj1 equals obj2?" + obj1.equals (obj2)); System.out.println ("obj2 equals obj3?" + obj2.equals (obj3)); System.out.println ("obj1's hashcode:" + obj1.hashCode ()) System.out.println ("obj2's hashcode:" + obj2.hashCode ()); System.out.println ("obj2's hashcode:" + obj3.hashCode ());}} / / output result: obj1==obj2? false obj2==obj3? true obj1's hashcode: 12677476 obj2's hashcode: 33263331 obj3's hashcode: 33263331
Why do I need to override the equals and hashCode methods?
If the hashCode and equals methods are not overridden, the address values are compared. Because the equals method used in Object is = =.
In our business system, what is sometimes needed to judge objects is not a strict sense of equality, but a kind of business object equality. In this case, the native equals method can not meet our needs.
So at this point we need to rewrite the equals method to meet the needs of our business system. So why do you need to override the equals method when you rewrite the hashCode method?
Let's first take a look at the general convention of Object.hashCode.
During the execution of an application, if the information used to compare an object's equals method has not been modified, then the hashCode method is called on the object multiple times, and it must always return the same integer. This integer can be different during multiple execution of the same application, that is, the integer returned by this application execution may be inconsistent with that returned by the next execution.
If two objects are equal according to the equals (Object) method, then calling the hashCode method of either object must produce the same integer result.
If two objects are not equal according to the equals (Object) method, calling the hashCode method of either of the two objects does not require different integer results. However, programmers should be aware of the fact that it is possible to improve the performance of hash tables (hash table) by producing very different integer results for unequal objects.
If only the equals method is overridden but not the hashCode method, the second rule of the convention is violated: equal objects must have equal hash codes (hashCode)
Also for classes such as HashSet and HashMap that are implemented based on hash values (hash). The underlying processing mechanism of HashMap is to save the data in an array (Node [] table), the key of which is the processing of array subscript. The subscript of the array is determined based on the return value of the hashCode method of the passed element and then XOR with a specific value.
If there is already a value in the array position, and the passed key value is equal, it will not be processed, if it is not equal, the original value will be overwritten, and if there is no entry in the array position, it will be inserted and added to the corresponding linked list. The existence of a check key is also determined based on the hashCode value. Therefore, if hashCode is not rewritten, it may cause HashSet and HashMap to fail to function properly.
If we save a custom object to HashMap or HashSet and similar implementation classes, if the properties of the object participate in the calculation of the hashCode, then we cannot modify the properties of the hashCode calculation of the object parameter. It is possible that the element cannot be removed, resulting in a memory leak.
Extend:
When overriding the equals method, you need to follow the following general conventions:
1. Reflexivity.
For any reference value xmeme x.equals (x) must be true.
2. Symmetry.
For any reference values x and y, if and only if y.equals (x) returns true, x.equals (y) must also return true.
3. Transitivity.
For any reference values x, y, and z, if x.equals (y) returns true and y.equals (z) also returns true, then x.equals (z) must also return true.
4. Consistency.
For any reference values x and y, if the object used for equals comparison has not been modified, the call to x.equals (y) returns either true or false consistently.
5. False must be returned for any non-null reference value xmemx.equals (null).
The approximate way to override the hashCode method:
A. Save some non-zero constant value, such as 17 (preferably prime), in a variable of type int called result.
B. For each key field f in the object (each field considered in the value equals method), complete some steps:
1. Calculate the hash of int type for this field? C:
1), if the field is of type boolean, calculate (f? 0:1).
2), if the field is of type byte, char, short, or int, calculate (int) f.
3) if the field is of type float, Float.floatToIntBits (f) is calculated.
4), if the field is of type long, calculate (int) (f ^ (f > 32)).
5) if the field is of type double, calculate Double.doubleToLongBits (f) to get a value of type long, and then follow step 4 to calculate the hash value for the long value.
6) if the domain is an object reference, and the equals method of the class compares the domain by recursively calling equals, then hashCode is also recursively called for the domain. If a more complex comparison is required, a "canonical representation" is calculated for the domain, and then hashCode is called for this normal form representation. If the value of this field is null, 0 (or some other constant) is returned
7) if the field is an array, each element is treated as a separate field. That is, apply the above rules recursively, calculate a hash code for each important element, and then combine these hash values according to the following steps.
2. According to the following formula, combine the hash code C calculated in step 1 into result:
Result = 31*result+c.
C. Return result.
D. After writing the hashCode method, ask yourself "whether equal instances have equal hash codes". If not, find out the cause and modify it.
On why to rewrite hashCode and equals methods to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.