Hashcode Methods for Java

Source: Internet
Author: User

There are many people who have studied Java for a long time, but have not understood the role of the Hashcode method,
Let me explain it to you. First, to understand the role of hashcode, you must first know the collection in Java.
In general, there are two types of collections (Collection) in Java, one is list, and the other is set.
Do you know the difference between them? The elements within the set are ordered, the elements can be repeated, the latter elements are unordered, but the elements are not repeatable.
So here is a more serious problem: to ensure that the elements do not repeat, can two elements should be repeated 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 numerous when the element is many.
That is, if there are now 1000 elements in the collection, then the 1001th element joins the collection, it calls the Equals method 1000 times. This obviously will significantly reduce efficiency.
Thus, Java uses the principle of a hash table. The hash (hash) is actually a personal name, and because he proposes a hash algorithm, it is named after his name.
A hashing algorithm, also known as a hashing algorithm, is a direct assignment of data to an address based on a particular algorithm. If the hashing algorithm is explained in detail, it will require more articles, which I will not introduce here.
Beginners can understand that the Hashcode method actually returns the physical address of the object store (which may not actually be the case).
This way, when the collection is to add a new element, the Hashcode method of the element is called first, and then it can be positioned at 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 further comparison; if there is already an element in this position,
The Equals method that calls it is compared with the new element, and the same is not saved, and the other addresses are hashed.
So there is a conflict resolution problem here. In this way, the number of actual calls to the Equals method is greatly reduced, almost only one or two times.
So, Java for the Eqauls method and the Hashcode method are defined as:
1, if two objects are the same, then their hashcode value must be the same;

2. If the hashcode of two objects are the same, they are not necessarily the same

The objects mentioned above refer to the comparison with the Eqauls method.
You can of course not do it as required, but you will find that the same object can appear in the Set collection. 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. So you're going to say, isn't that the Equals method? Yes, these 2 methods are used to determine whether 2 objects are equal. But they are different. In general, the Equals method is called to the user, if you want to determine whether 2 objects are equal, you can rewrite the Equals method and then call in the code to determine whether they are equal. In simple terms, the Equals method is primarily used to judge whether 2 objects are not equal on the surface or on the content. For example, there is a student class, the attribute is only the name and gender, then we can think that as long as the name and gender equality, then say that the 2 objects are equal. Hashcode method General users do not call, such as in HashMap, because key is not repeatable, he in the judgment key is not repeated when the hashcode this method, but also used the Equals method. Here can not repeat is that equals and hashcode as long as there is a range! So simply put, hashcode is equivalent to an object encoding, as if the file of MD5, he and equals is the difference is that he returned to the int type, compared to not intuitive. We generally overwrite equals while covering the hashcodeto make their logic consistent . For example, if the name and gender are equal 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 are consistent. to physically determine whether 2 objects are equal, use = = to do so .

Turn from:

Http://www.cnblogs.com/batys/archive/2011/10/25/2223942.html

Hashcode Methods for Java

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.