In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the difference between Hashcode and equals in the Object class". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The default implementation in the equals Object class is: return this = = obj. That is, true is returned only if this and obj refer to the same object. The Hashcode method returns the hash code of the object, and the return value is the hash code of type int.
Equals:
The default implementation in the Object class is: return this = = obj. That is, true is returned only if this and obj refer to the same object.
Instead of verifying their uniqueness, we often need to use equals to determine whether two objects are equivalent or not. So when we implement our own class, we have to rewrite equals
By convention, equals meets the following rules.
Reflexivity: x.equals (x) must be true
For null: x.equals (null) must be false
Symmetry: x.equals (y) and y.equals (x) have the same results.
Transitivity: an and b equals, b and c equals, then an and c must also be equals.
Consistency: a change in the state of two objects during a runtime does not affect the decision result of equals, so the same result is returned no matter how many times equals is called during this runtime.
Hashcode:
This method returns the hash code of the object, and the return value is the hash code of type int.
The hash code of the object is to better support Java collection classes based on hash mechanism.
For example, Hashtable, HashMap, HashSet, etc.
With regard to the hashCode method, the consistent convention is:
Objects that override the euqls method must also override the hashCode () method.
If two objects return true after being called through equals, then the hashCode method of the 2 object must also return the same int hash code
If two objects return false through equals, their hashCode returns the same value. However, programmers must be aware that hashCode returns a unique hash code, which makes the hashtables that stores this object better.)
In the above example, the Test class object has two fields, num and data, which represent the state of the object, and they are also used as a basis for judgment in the equals method. Then, in the hashCode method, these two fields also participate in the operation of the hash value as the intermediate parameters of the hash operation. This is critical in order to follow: if there are two objects equals, then the hashCode must be the same rule.
In other words, the fields that participate in the equals function must also participate in the calculation of hashCode.
It makes sense that different objects in the same class return different hash codes. The typical way is to convert the hash code of the object based on the address of the object, but this is not the only required implementation for Java. It is not usually the best way to implement it.
Compared with the generally accepted implementation agreement of equals, the convention requirements of hashCode are easy to understand. There are two key points that the hashCode approach must follow. The third point of the agreement is actually the refinement of point 2, so let's take a look at the consistent convention requirements for the hashCode method.
First: during a run time, as long as the change in the object (field) does not affect the decision result of the equals method, the same hash code must be returned no matter how many times hashCode is called during this period.
Second: the hashCode of the two objects that return true through the equals call must be the same.
Third: the hash codes of the two objects that return false through equasl do not need to be different, that is, the return values of their hashCode methods allow the same situation.
To sum up: the equivalent (calling equals returns true) object must produce the same hash code. Objects that are not equivalent do not require different hash codes.
This is the end of the content of "what is the difference between Hashcode and equals in the Object class". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.