Java Hashcode Detailed

Source: Internet
Author: User

First, why should have hash algorithm

There are two types of collections in Java , one is list, and the other is set. The elements in the list are ordered and the elements can be duplicated. The set element is unordered, but the element is not repeatable. If you want to ensure that the elements are not duplicated, should the two elements be repeated according to what to judge? Use 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 large. That is, if there are 1000 elements in the set, then the 1001th element joins the collection, it calls the Equals method 1000 times. This obviously will significantly reduce efficiency. So Java uses the principle of a hash table. The hash is a personal name, because the concept of the hashing algorithm he proposed is named after his name.

Second, theprinciple of hash algorithm

Hashcode's Official document definition:

The general agreement of Hashcode is:

[Plain]View PlainCopy print?
    1. 1. During the execution of a Java application, the same integer must be returned consistently when the Hashcode method is called multiple times on the same object, as long as the information used by the equals comparison operation of 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.
    2. 2. 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.
    3. 3. 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 does not necessarily 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.



[Plain]View PlainCopyprint?
  1. 1.hashcode is used to find, if you have learned the data structure you should know that in the Find and sort this chapter has
  2. For example, there is such a location in memory
  3. 0 1 2 3 4 5 6 7
  4. And I have a class, this class has a field called ID, I want to put this class in one of the above 8 locations, if not hashcode and arbitrary storage, then when looking for the need to go to these eight locations to find, or using a two-way algorithm.
  5. But if you use hashcode that will improve the efficiency a lot.
  6. 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.
  7. 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.
  8. 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.
  9. So. overriding Equals (), why rewrite Hashcode ()?
  10. Think about it, you have to find something in a bucket, you have to get the bucket first, you don't have to rewrite hashcode () to find

When the set receives an element, it calculates the hashcode according to the information of the object, and sees which interval it belongs to, and calls the Equeals method in this interval.

It does improve efficiency. But a problem: if two objects equals equal, but not in an interval, there is no chance to compare, it will be considered a different object. So Java for the Eqauls method and the Hashcode method are defined as:

1 if two objects are the same, their hashcode values must be the same. Also tells us to rewrite the Equals method, be sure to override the Hashcode method.

2 If the hashcode of two objects are the same, they are not necessarily the same, and the objects here refer to the comparison using the Eqauls method.

Analysis of the method of Hashcode

Hash table This data structure presumably most people are not unfamiliar, and in many places will use hash tables to improve the search efficiency. There is a method in the object class in Java:

[Java]View PlainCopyprint?
    1. Public native int hashcode ();

Why does the object class need such a method? What role does it have? Today we are going to discuss the Hashcode method specifically. According to the declaration of this method, the method returns a numeric value of type int and is a local method, so no specific implementation is given in the object class.

For programming languages that contain container types, hashcode is basically involved. As in Java, the Hashcode method is primarily intended to work with hash-based collections, such as HashSet, HashMap, and Hashtable.

Why do you say that? Consider the case of how to determine if an object already exists in the collection when it is inserted into the collection. (Note: Duplicate elements in the collection are not allowed to exist)

Perhaps most people would have thought of calling the Equals method to make comparisons on a case by case basis. However, if there are already 10,000 or more data in the collection, if the Equals method is used to compare each other, efficiency must be a problem. At this point the function of the Hashcode method is reflected, when the collection to add a new object, the first call the object's Hashcode method, to get the corresponding hashcode value, In fact, in the implementation of HASHMAP will use a table to save the hashcode value of the object that has been stored in, if the table does not have the hashcode value, it can be stored directly in, no further comparison, if there is a hashcode value, The Equals method that calls it is compared with the new element, the same is not saved, and the other addresses are hashed, so there is a conflict resolution problem, so the number of actual calls to the Equals method is greatly reduced. The Hashcode method in Java is to map the object-related information (such as the object's storage address, the object's field, and so on) into a numeric value, which is called a hash, according to certain rules. The following code is a concrete implementation of the Put method in Java.util.HashMap:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Public V put (K key, V value) {

if (key = = null)

return Putfornullkey (value);

int hash = hash (Key.hashcode ());

int i = indexfor (hash, table.length);

for (entry<k,v> e = table[i]; E! = null; e = e.next) {

Object K;

if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)) {

V oldValue = E.value;

E.value = value;

E.recordaccess (this);

return oldValue;

}

}

modcount++;

AddEntry (hash, key, value, I);

return null;

}

The Put method is used to add a new element to the HashMap, and from the concrete implementation of the Put method, the Hashcode method is called to get the hashcode value of the element, and then see if the hashcode value exists in the table. If present, call the Equals method to re-determine if the element exists, update the value if it exists, or add the new element to HashMap. As can be seen here, the Hashcode method exists to reduce the number of calls to the Equals method, thus improving program efficiency.

So some people would say that two objects can be judged directly according to the hashcode value of equality? It is certainly not possible, because different objects may generate the same hashcode value. Although it is not possible to determine whether two objects are equal according to the Hashcode value, two objects can be judged directly from the hashcode value, and if the hashcode value of two objects is unequal, it must be two different objects. If you want to determine whether two objects are truly equal, you must pass the Equals method.

That is to say, for two objects, if the result of calling the Equals method is true, then the hashcode value of the two objects must be equal;

If the Equals method results in False, the hashcode value of the two objects may not be different;

If the hashcode value of two objects is not equal, the result of the Equals method must be false;

If the hashcode value of two objects is equal, the result of the Equals method is unknown.

when overwriting equals, always overwrite hashcode

What happens if you do not overwrite, this violates the second rule, the equal object must have the same hash code

If you do not write, even if the object is equal, two different hash codes are returned.

Java Hashcode Detailed

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.