Detailed Java Hashcode () function to find the object hash code value _java

Source: Internet
Author: User
Tags object object set set

Understand
The function of Hashcode () is to get the hash code, also known as the hash code, which is actually returning an int integer. The purpose of this hash code is to determine the index position of the object in the hash table.
Hashcode () is defined in the JDK's object.java, which means that any class in Java contains the hashcode () function.
Although, each Java class contains the hashcode () function. However, just when you create and a "hash list" of a class (for the hash list, see the following description), the hashcode () of the class is useful for determining the position of each object of the class in the hash table, in other cases (for example, creating a single object of a class, or creating an array of objects of a class, and so on), The Hashcode () of the class has no effect.
The above hash table refers to a class that is essentially a hash table in a Java collection, such as Hashmap,hashtable,hashset.
That is to say: Hashcode () is useful in a hash table, and in other cases it is useless. The purpose of hashcode () in a hash table is to get the hash code of an object, and then to determine the position of the object in the hash table.
As we all know, the hash table stores the key value pair (Key-value), which is characterized by the ability to quickly retrieve the corresponding "value" according to the "key". That's where the hash code is used!
The nature of the hash table is implemented through arrays. When we want to get a "value" in the hash table, we are actually getting an element of a location in the array. The position of the array is obtained by the "key"; Further, the position of the array is computed by the hash code corresponding to the "key".
Below, we take hashset as an example to explain the role of hashcode () in depth.
Suppose that there are already 1000 elements in the hashset. When you insert the 1001th element, what do you need to do with it? Because HashSet is a set set, it allows for duplicate elements.
"Compare the 1001th element to the preceding 1000 elements one by one"? Obviously, this efficiency is equally low. The hash table solves this problem by calculating the position of the element in the hash table based on the hash code of the element, and then inserting the element into that position. For the same element, nature is only saved one.
So, if two elements are equal, their hash codes must be equal, but the reverse is not necessarily true. In the hash table,
1, if two objects are equal, then their hashcode () value must be the same;
2, if two objects hashcode () are equal, they are not necessarily equal.
Note: This is the case in the hash table. This must be true in a non-hash table!

Example
let's look at a concrete example,

public class Hashtest { 
  private int i; 
 
  public int Geti () {return 
    i; 
  } 
 
  public void SetI (int i) { 
    this.i = i; 
  } 
 
  public int hashcode () {return 
    i% 
  } 
 
  Public final static void main (string[] args) { 
    hashtest a = new Hashtest (); 
    Hashtest B = new Hashtest (); 
    A.seti (1); 
    B.seti (1); 
    set 
 

The result of this output:

True 
false 
[com.ubs.sae.test.hashtest@1, com.ubs.sae.test.hashtest@1] 

The above example, we just rewrite the Hashcode method, and from the results above we can see that although the two objects are hashcode equal, but the actual two objects are not equal; we didn't rewrite the Equals method, Then the object default Equals method is called, which is to compare the references of two objects to the same, showing that this is two different objects, and the references of two objects must be indeterminate. Here we put the generated objects in the HashSet, and the HashSet only holds the unique object, i.e. the same object (for the Equals method) will only hold one, but this is actually two objects a,b are put into the hashset, So HashSet lost the meaning of his own.
At this point we add the Equals method:

public class Hashtest {private int i; 
  public int Geti () {return i; 
  } public void SetI (int i) {this.i = i; } <span style= "color: #3366FF;" 
    ><strong>public Boolean equals (Object object) {if (object = = null) {return false; 
    } if (object = = this) {return true; } if (! ( 
    Object instanceof Hashtest)) {return false; 
    Hashtest other = (Hashtest) object; 
    if (other.geti () = = This.geti ()) {return true; 
  return false; 
  }</strong></span> public int hashcode () {return I% 10; 
    Public final static void main (string[] args) {Hashtest a = new Hashtest (); 
    Hashtest B = new Hashtest (); 
    A.seti (1); 
    B.seti (1); 
    set 

The results will be as follows:

True 
true 
[com.ubs.sae.test.hashtest@1] 

As we can see from the result, two objects are now completely equal, and only one object is stored in the hashset.

Summarize
1, the existence of hashcode is mainly used to find the shortcut, 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 hashcode must be the same;

3, if the object's Equals method is rewritten, then the object's hashcode also as far as possible rewrites, and produces hashcode uses the object, must and Equals method uses consistent, otherwise 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 to show that these two objects in the hash storage structure, such as Hashtable, they "stored in the same basket ”。

Related Article

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.