How to Use the hashCode Method

Source: Internet
Author: User

First, to understand the role of hashCode, you must first know the set in Java.
In general, collections in Java have two types: List and Set.
Do you know the differences between them? The elements in the former set are ordered, and the elements can be repeated. The latter elements are unordered, but the elements cannot be repeated.
So here is a serious problem: to ensure that the elements are not repeated, what is the basis for determining whether the two elements are repeated?
This is the Object. equals method. However, if each added element is checked once, when there are many elements, the number of times that the elements added to the set are compared is very large.
That is to say, if there are already 1000 elements in the Set, it will call the 1,001st equals method when 1000 elements are added to the set. This will obviously greatly reduce the efficiency.
Therefore, Java uses the principle of hash tables. Hash is actually a personal name. As he proposed a Hash algorithm, he named it.
A hash algorithm is also called a hash algorithm. It directly specifies an address based on a specific data algorithm. If you want to describe the hash algorithm in detail, more articles are required. I will not describe it here.
As a beginner, The hashCode method actually returns the physical address of the Object Storage (which may not actually be ).
In this way, when a set needs to add a new element, the hashCode method of this element is called first, and the physical location where it should be placed can be located at once.
If there are no elements in this position, it can be directly stored in this position without any comparison. If there are already elements in this position,
You can call its equals method to compare it with the new element. If it is the same, it will not be saved. If it is different, other addresses will be hashed.
Therefore, there is a conflict resolution problem. In this way, the number of actually called equals methods is greatly reduced, and it takes almost one or two times.
Therefore, Java specifies the eqauls method and hashCode method as follows:
1. If the two objects are the same, their hashCode values must be the same; 2. If the two objects have the same hashCode, they are not necessarily the same as the objects mentioned above. They are compared using the eqauls method.
Of course you can do it as required, but you will find that the same object can appear in the Set. At the same time, the efficiency of adding new elements will be greatly reduced. Hashcode is used to identify whether two objects are equal.
Then you will say, isn't there another equals method? Yes. Both methods are used to determine whether two objects are equal. But they are different. Generally, the equals method is called for users. If you want to determine whether two objects are equal,
You can override the equals method and call it in the code to determine whether they are equal. To put it simply, the equals method is mainly used to determine whether two objects are equal on the surface or content. For example, there is a student,
Only the name and gender of the attribute can be considered as long as the name and gender are equal, the two objects are equal. The hashcode method is generally not called. For example, in hashmap, keys cannot be repeated,
He judges the hashcode method when judging whether the key is repeated, and also uses the equals method. It cannot be repeated here. It means that equals and hashcode can be used as long as there is a difference! In short
When the encoding of an object is like md5 in the file, it is different from equals because it returns the int type, which is not intuitive. We generally need to overwrite the hashcode while overwriting equals so that their logic is consistent. For example,
In the preceding example, if the name and gender are equal, even if the two objects are equal, the hashcode method also needs to return the hashcode value of the name and the hashcode value of the gender. Logically, they are consistent.
To physically determine whether two objects are equal, use =.

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.