In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the difference between the usage of equals (), = = and hashCode () in Java. In daily operation, I believe that many people have doubts about the difference between the usage of equals (), = = and hashCode () in Java. The editor has consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the questions about the difference between the usage of equals (), = = and hashCode () in Java! Next, please follow the editor to study!
Equals ()
The purpose of equals () is to determine whether two objects are equal. It is defined in the Object class of JDK. The source code is as follows
Because the Object class is the base class of all classes, all Java classes have an equals () method. It's just that the default equals () method is equivalent to = =. By default, the equals () method in the Object class distinguishes whether two objects are equal by determining whether their addresses are equal (that is, whether they are the same object).
Therefore, we usually override the equals () method. If a class does not override the equals () method, when comparing two objects through its equals (), you are actually comparing whether the two objects are the same object. At this point, it is equivalent to comparing the two objects through "=". When we override the equals () method of the class, we use the equals () method to compare whether the two objects are equal. The content of the two objects is compared to be equal, and the equals () method returns true; if equal, otherwise it returns fasle.
Java's equals () has five features:
Symmetry: if x.equals (y) returns "true", then y.equals (x) should also return "true".
Reflectivity: x.equals (x) must return "true".
Analogy: if x.equals (y) returns "true" and y.equals (z) returns "true", then z.equals (x) should also return "true".
Consistency: if x.equals (y) returns "true", as long as the x and y contents remain the same, no matter how many times you repeat x.equals (y), the return is "true".
Non-null: x.equals (null), always returns "false"; x.equals (objects of a different type from x) always returns "false".
Therefore, when we rewrite the equals () method, be sure to verify that the above five features are met. These five features are also known as the five rewriting rules of equals ().
= =
The function of = = is to determine whether the addresses of two objects are equal. That is, it is determined that two objects do not try the same object.
During the interview, you will basically ask what is the difference between the equals method and the'= = 'operator. If you answer, "equals compares the contents of the object, while'= 'compares the address of the object." So this answer is strictly wrong, or at least not entirely correct.
From the previous content, we know that the implementation of the equals method in Object also directly uses the'= = 'operator for comparison. Therefore, the answer to this question must be rigorous, plus pre-conditions.
HashCode ()
The purpose of hashCode () is to get a hash code, also known as a hash code; it actually returns an int integer. It is commonly used in hash collections. HashCode () is uniformly defined in JDK's Object, which means that any class in Java contains the hashCode () function.
So why rewrite equals () and hashCode () at the same time? It is because collections such as HashMap,Hashtable,HashSet cannot guarantee that hash does not conflict when they process the hash code of key objects to calculate the storage location. In addition, when the equals () method is equal, hashCode () is not necessarily equal.
But when using a hash table, we require that if two objects have the same equals (), then their hashCode () values must be the same. If two objects hashCode () are equal, their equals () is not necessarily equal.
Because in the hash table, hashCode () is equal, that is, the hash values of the two key-value pairs are equal. However, if the hash is equal, it does not necessarily lead to the equality of key-value pairs. Add: "two different key-value pairs, the hash value is equal." this is the hash conflict.
We know that HashSet is a collection that does not allow repeating elements to be stored, but if you do not override equals () and hashCode () of element classes, HashSet is basically meaningless. This is also the reason why many students misuse HashSet, but do not use the results they want.
At this point, the study of "the difference between the usage of equals (), = = and hashCode () in Java" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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
[oracle@tdsogg tmp] $crontab-lump 5 * sh / tmp/delete.sh*/5 * / tmp/delete.sh5 * sh /
© 2024 shulou.com SLNews company. All rights reserved.