In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to rewrite the Equals method". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to rewrite the Equals method".
The existence of equals and hashcode
In fact, each class has an equals method and a hashcode method. Because all classes inherit from the Object class. The definition in the Object class is as follows:
Public boolean equals (Object obj) {return (this = = obj);} public native int hashCode ()
Intuitively, you can see that the equals method compares references to objects by default, directly comparing them with "=". The hashCode method is a native method, and the return value is an integer.
Neither of these methods is modified by final and can be rewritten.
The equals () and hashCode () methods are overridden for classes that we often use, such as String, Math, Integer, Double, and so on.
Equals () method
The equals () method is used to determine whether two objects are equal. Object implements the equals method by default, but it obviously doesn't quite meet the needs of personalization, so it often needs to be rewritten. For example, the commonly used String class overrides the equals method as follows:
/ / override the equals method public boolean equals (Object anObject) {if (this = = anObject) {return true;} if (anObject instanceof String) {String anotherString = (String) anObject; int n = value.length; if (n = = anotherString.value.length) {char v1 [] = value; char v2 [] = anotherString.value; int I = 0 While (n return true; -! = 0) {if (v1 [I]! = v2 [I]) return false; iTunes;} return true;}} return false;}
The comparison here is no longer a simple address comparison. First of all, compare by address, if the address is the same, then it must be the same object. Compare the contents of the char array if the addresses are different, and return true if they are exactly equal.
The characteristics of equals () method
There are comments on the equals method of the Object class that describe some of the features that the equals () method needs to meet:
Reflexivity (reflexive). For any reference that is not null, the reference value xMagnex.equals (x) must be true.
Symmetry (symmetric). For any reference values x and y that are not null, y.equals (x) is also true if and only if x.equals (y) is true.
Transitivity (transitive). For any reference values x, y, and z that are not null, if x.equals (y) is true and y.equals (z) is true, then x.equals (z) must be true
Consistency (consistent). For any reference values x and y that are not null, if the object information used for equals comparison has not been modified, x.equals (y) either consistently returns true or false when called multiple times.
Returns false for any reference value xj x.equals (null) that is not null
Compared with the above characteristics, we find that the Object method directly compares two reference addresses, and only the two addresses are the same, that is to say, the equivalence relation with the greatest possibility of difference.
The equals method of String includes not only the same application address, but also the same string value stored in it. That is to say, although they are two String objects, their string values are equal, so the result returned by the equals method is true. This is what we call "the equals method compares values" in most cases.
Because the default special case of Object's equals method exists, when there is no custom equals method, we cannot say that the equals method compares specific values, while "= =" compares references.
HashCode () method
The hashCode () method returns a hash code value of the object. This method is used in hash tables, such as HashSet, HashMap.
HashCode () is a native method that returns a value of type integer and can be overridden.
The native hashCode () method in Object returns the address of the object in memory as a hash code, which ensures that the return values of different objects are different.
Also take the string class as an example, whose hashCode method is:
/ / override the hashCode method public int hashCode () {int h = hash; if (h = = 0 & & value.length > 0) {char val [] = value; for (int I = 0; I)
< value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; } 上述hash值的计算注释中有说明,基本公式为:s[0]*31^(n-1) + s[1]*31^(n-2) + … + s[n-1]。 其中, s[i]是字符串的第i个字符,n是字符串的长度,^表示求幂(空字符串的哈希码为0)。 计算过程中使用数字31,主要有以下原因: 1、由于质数的特性,它与其他数字相乘之后,计算结果唯一的概率更大,哈希冲突的概率更小。 2、使用的质数越大,哈希冲突的概率越小,但是计算的速度也越慢;31是哈希冲突和性能的折中,实际上是实验观测的结果。 3、JVM会自动对31进行优化:31 * i == (i >> 32))
(4) if it is a float value, calculate Float.floatToIntBits (f)
(5) if it is a long value, Double.doubleToLongBits (f) is calculated, and then the returned result is long, and then rule (3) is used to process long to get int.
(6) if it is an object application, if the comparison method of recursive call is adopted in the equals method, then the way of recursive call to hashCode is also adopted in hashCode. Otherwise, you need to calculate a normal form for this field. For example, if the value of this field is null, then the value of hashCode is 0.
(7) if it is an array, it needs to be treated as a separate field for each element. The java.util.Arrays.hashCode method includes the hashCode calculation of eight basic types of arrays and reference arrays, and the algorithm is the same as above.
Finally, merge the hash code of each field into the hash code of the object.
Thank you for your reading, the above is the content of "how to rewrite the Equals method", after the study of this article, I believe you have a deeper understanding of how to rewrite the Equals method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.