Role of hashcode

Source: Internet
Author: User
Tags rehash

Source: http://blog.chenlb.com/2009/09/hashcode-effect.html

What is the role of Java object hashcode? Hash Tables (hash tables) and hash functions that can be associated with data structures. Object. hashcode () is a hash function used to calculate hash values to implement a data structure such as a hash table.

Take a look at the hash table structure:

Hash table

When an object is stored in an array, the hash value obtained by hashcode is used to calculate the index location of the array (usually the remainder operation), and then the index location is used for access. When the index locations calculated by multiple objects are the same (hash conflicts), they are saved in a linked list. How can we ensure that we get the conflict from ourselves? The object. Equals () method is required.

Therefore, to store objects in a data structure similar to a hash table (for example, hashset), hashcode and equals must be implemented in pairs.

Java object hashcode API

Returns the hash value of the object. This method provides some advantages for the hash table, such as the hash table provided by Java. util. hashtable.
The general protocol of hashcode is:
  • Multiple calls on the same object during Java application executionHashcodeThe same integer must be returned in the same way, provided thatEqualsThe information used in the comparison is not modified. This integer does not need to be consistent from one execution of an application to another execution of the same application.
  • IfEquals (object)Method. If the two objects are equal, the hashcode method must be called on each object of the two objects to generate the same integer result.
  • The following situationsNoRequired: If two objects are not equal according to the equals (Java. Lang. Object) method, callHashcodeThe method will certainly generate different integer results. However, programmers should know that generating different integer results for unequal objects can improve the performance of the hash table.

In factObjectThe hashcode method defined by the class does return different integers for different objects. (This is generally achieved by converting the internal address of the object into an integer, but this implementation technique is not required by the javatm programming language .)

According to the above agreement, when the state of an object (these states are not necessarily all fields, depending on the business) is not changed, multiple calls to hashcode must be equal. However, different objects can have the same hashcode, but try to make different objects have different hashcode to improve the performance of the hash table.

Let's take a look at the get and put implementations of Java hashtable:

  1. Public synchronized v get (Object key ){
  2. Entry tab [] = table;
  3. Int hash = key. hashcode ();
  4. Int Index = (hash & 0x7fffffff) % tab. length;
  5. For (Entry <K, V> E = tab [Index]; e! = NULL; E = E. Next ){
  6. If (E. Hash = hash) & E. Key. Equals (key )){
  7. Return e. value;
  8. }
  9. }
  10. Return NULL;
  11. }

First, search for the index location index of the array based on key. hashcode, and ensure that hash & 0x7fffffff is a positive number. Then follow the steps below to find the same hash and equals. The longer the conflict chain, the worse the performance.

See the put method:

  1. Public synchronized v put (K key, V value ){
  2. // Make sure the value is not null
  3. If (value = NULL ){
  4. Throw new nullpointerexception ();
  5. }
  6. // Makes sure the key is not already in the hashtable.
  7. Entry tab [] = table;
  8. Int hash = key. hashcode ();
  9. Int Index = (hash & 0x7fffffff) % tab. length;
  10. For (Entry <K, V> E = tab [Index]; e! = NULL; E = E. Next ){
  11. If (E. Hash = hash) & E. Key. Equals (key )){
  12. V old = E. value;
  13. E. value = value;
  14. Return old;
  15. }
  16. }
  17. Modcount ++;
  18. If (count> = threshold ){
  19. // Rehash the table if the threshold is exceeded
  20. Rehash ();
  21. Tab = table;
  22. Index = (hash & 0x7fffffff) % tab. length;
  23. }
  24. // Creates the new entry.
  25. Entry <K, V> E = tab [Index];
  26. Tab [Index] = new entry <K, V> (hash, key, value, e );
  27. Count ++;
  28. Return NULL;
  29. }

First check whether there are the same ones. If yes, replace them. If the array space is not enough, the allocated space will be changed and the data will be rehashed and saved. Finally, insert it on the conflicting link.

Other useful connections: Analysis
JDK source code research Hash Storage Mechanism

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.