The role of Hashcode in Java

Source: Internet
Author: User

The following is an official document definition for hashcode:

The Hashcode method returns the hash code value of the object.     This method is supported to provide some advantages to the hash table, for example, the Hashtable provided by Java.util.Hashtable. The general contract for Hashcode is that during Java application execution, when the Hashcode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used in the Equals comparison on the object has not been modified.   The integer does not need to be consistent from one execution of an application to another execution of the same application.   If two objects are equal according to the Equals (object) method, calling the Hashcode method on each object in two objects must produce the same integer result. The following conditions are not required: If two objects are not equal according to the Equals (Java.lang.Object) method, then calling the Hashcode method on any of the two objects must produce a different integer result.   However, programmers should know that generating different integer results for unequal objects can improve the performance of the hash table. In fact, the Hashcode method defined by the object class does return different integers for different objects. (This is typically done by converting the object's internal address to an integer, but the JAVATM programming language does not require this implementation technique.) When the Equals method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the Hashcode method, which declares that the equality object must have an equal hash code.

The above-mentioned official document definition, we can be drawn into the following key points:

1, the existence of hashcode is mainly used to find the fast, such as Hashtable,hashmap, Hashcode is used in the hash storage structure to determine the storage address of the object;

2, if two objects are the same, is applicable to the Equals (Java.lang.Object) method, then the two objects must be the same hashcode;

3, if the object's Equals method is overridden, then the object's hashcode also try to rewrite, and produce hashcode used objects, must be consistent with the use of the Equals method, otherwise it will violate the above mentioned 2nd;

4, two objects of the same hashcode, does not necessarily mean that two objects are the same, that is not necessarily applicable to the Equals (Java.lang.Object) method, only can explain that the two objects in the hash storage structure, such as Hashtable, they " stored in the same basket. "

1.hashcode is used to find, if you have learned data structures you should know, in the Find and sort this chapter has such a place in memory such as 0 1 2 3 4 5 6 7 and I have a class, this class has a field called ID, I want to store this class in one of the above 8 locations, if not  Hashcode and arbitrarily stored, then when the search will need to go to these eight locations to find, or use a two-way algorithm.  But if you use hashcode that will improve the efficiency a lot. We have a field called ID in this class, then we define our hashcode as id%8, and then we store our class in the place where we get the remainder. For example, our ID is 9, 9 except 8 of the remainder is 1, then we put the class exists 1 this position, if the ID is 13, the remainder is 5, then we put the class at 5 this position.    In this way, the remainder can be found directly by ID in addition to 8 when the class is looked up. 2. But if the two classes have the same hashcode what to do (we assume that the ID of the class above is not unique), such as 9 divided by 8 and 17 divided by 8, the remainder is 1, then this is not legal, the answer is: can do so. So how do you judge it?  At this point, you need to define equals.  In other words, we first determine whether two classes are stored in a bucket by hashcode, but there may be many classes in the bucket, then we need to find our class in this bucket by equals. So.  overriding Equals (), why rewrite Hashcode ()? If you want to find something in a bucket, you have to find the bucket first, you don't have to rewrite hashcode () to find the bucket, the Light rewrite equals () What's the use?

Two object values are the same (X.equals (y) ==true), but can have different hash code, this sentence right? Why?


The role of Hashcode in 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.