JAVA hashcode () and equal () methods

Source: Internet
Author: User

Many people have studied Java for a long time, but have not understood the role of the Hashcode method,
Let me explain. First of all, to understand the role of hashcode, you have to know the collection in Java first.
In general, the set in Java (Collection) has two classes, one is list, and the other is set.
Do you know the difference between them? The elements in the former set are ordered, the elements can be duplicated, the latter elements are unordered, but the elements cannot be duplicated.
So here is a more serious problem: to ensure that the elements do not repeat, can two elements of repetition should be based on what to judge.
This is the Object.Equals method. However, if each additional element is checked once, the number of elements that are added to the collection is much more frequent when the elements are many.
That is, if there are now 1000 elements in the collection, then the 1001th element will call the Equals method 1000 times when it joins the collection. This will obviously greatly reduce efficiency.
As a result, Java uses the principle of a hash table. Hash is actually a personal name, and since he proposes the concept of a hashing algorithm, it is named after him.
Hash algorithms, also known as hashing algorithms, are used to assign data directly to an address according to a specific algorithm. If the hashing algorithm is explained in detail, it will require more space for the article, I will not introduce it here.
Beginners can understand that the Hashcode method actually returns the physical address of the object store (which may not actually be).
This way, when the collection wants to add a new element, it first calls the Hashcode method of the element, and then it can navigate to the physical location where it should be placed.
If there is no element in this position, it can be stored directly in this position without any comparison; if there is already an element in this position,
The Equals method that calls it is compared to the new element, and the same is not saved, and the other addresses are hashed out.
So there is a problem of conflict resolution here. The number of actual calls to the Equals method is greatly reduced, almost one or two times.
So, Java for the Eqauls method and the Hashcode method is this stipulation:
1, if two objects are the same, then their hashcode value must be the same; 2, if two objects have the same hashcode, they are not necessarily the same as the object mentioned above are compared with the Eqauls method.
You can of course not do it as requested, but you will find that the same object can appear in the set set. At the same time, the efficiency of adding new elements will be greatly reduced.

Hashcode This method is used to identify whether 2 objects are equal. Then you will say, is not there is the method of equals. Yes, these 2 methods are used to determine whether 2 objects are equal. But they are different. In general, this method of equals is called to the user, and if you want to determine whether 2 objects are equal, you can rewrite the Equals method and then call in the code to determine if they are equal. In simple terms, the Equals method is primarily used to determine whether 2 objects are equal or not on the surface or on the content. For example, there is a student class, the attribute is only the name and sex, then we can think as long as the name and gender equality, then say that these 2 objects are equal. Hashcode method The general user does not go to call, for example in HashMap, because key is not repeatable, he judged the key is not repeated time to judge the hashcode this method, but also used the Equals method. This can not be repeated here is that equals and hashcode as long as there is a range. So in simple terms, hashcode is equivalent to an object encoding, as if the file in the MD5, he and equals are different is that he returned to the int type, compared to not intuitive. We generally cover equals while also covering hashcode, so that their logic is consistent. For example, if the name and gender equality even if 2 objects are equal, then the Hashcode method also returns the Hashcode value of the name plus the hashcode value of the gender, so logically they agree. To physically determine whether 2 objects are equal, use = = on it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.