HashMap's internal implementation mechanism, how the hash is achieved, when rehash

Source: Internet
Author: User
Tags rehash

The internal implementation mechanism of 1.HASHMAP

HashMap is the realization of hash table in data structure, hash list is also called Hash. A hash table is a data structure that accesses its corresponding value based on key code keys, which maps the key code to a location in the table by a mapping function to access the value of that location, thus speeding up the search. This mapping function is called the hash function, and the array that holds the record is called the hash table.

In Java, HashMap's internal implementation combines the advantages of linked lists and arrays, and the data structure of the linked nodes is Entry<k,v>, and each entry object contains a reference to the next entry type object, as shown in the following code:

Static Class Entry<k,v> implements Map.entry<k,v> {        final K key;        V value;        Entry<k,v> Next; The entry type has a reference to its own type, pointing to the next entry        final int hash;   
...}

As you can see in the HashMap constructor, the Entry table is declared for the array, as shown in the following code:

Public HashMap () {          this.loadfactor = default_load_factor;          threshold = (int) (default_initial_capacity * default_load_factor);          Table = new Entry[default_initial_capacity];          Init ();      }  

In the above constructor, the default default_initial_capacity value of 16,default_load_factor is 0.75.

When put an element into the HASHMAP, its internal implementation is as follows:

Public V put (K key, V value) {          if (key = = null)              return Putfornullkey (value);          int hash = hash (Key.hashcode ());          int i = indexfor (hash, table.length);          ...    }  

You can see that the put function uses a hash function to get the hashes, it should be pointed out that the Hashtable is implemented directly using the hashcode as the hash value, so the use of HashMap instead of Hashtable has a certain optimization.

The two functions used in the PUT function hash and indexfor are actually as follows:

static int hash (int h) {          //This function ensures, hashcodes that differ-only to          //constant multiples at each Bit position has a bounded          //number of collisions (approximately 8 at default load factor).          H ^= (H >>>) ^ (h >>> N);          Return h ^ (H >>> 7) ^ (H >>> 4);      }  
     /**      * Returns index for hash code h.      */      static int indexfor (int h, int length) {          return H & (length-1);      }  

As to why the hash function is designed, this involves the design problem of the specific hash function, and the time complexity of the hashing algorithm should be considered, and the optimal time and space can be obtained by making the value of each position in the array possible.

The Indexfor function uses a very ingenious and operation to limit the index value within Length-1.

Of course, the hash function exists in the case of conflict, the same key corresponding to the hash value may be the same, when the hash value of the same element will be stored by the link, the HashMap get method in the acquisition of value when the linked list is traversed, the key value matches the value to take out.

Implementation of the 2.Hash

The main problem is hashing algorithms and conflict resolution.

3. When rehash

In introducing HashMap's internal implementation mechanism, two parameters were mentioned, and Default_initial_capacity and default_load_factor,default_initial_capacity were the capacity of the table array. Default_load_factor is an impact factor set to maximize the avoidance of hash collisions, increase hashmap efficiency, and multiply it by default_initial_capacity to get a threshold threshold, When the capacity of HashMap to threshold need to expand, this time will be rehash operation, you can see the implementation of the AddEntry function below, when the size reaches threshold will call resize function to expand.

void AddEntry (int hash, K key, V value, int bucketindex) {  ntry<k,v> e = Table[bucketindex];        Table[bucketindex] = new entry<k,v> (hash, key, value, e);        if (size++ >= threshold)            Resize (2 * table.length);    }  

Rehash operations are required during the expansion process, which is time-consuming and should be avoided in practice.

HashMap's internal implementation mechanism, how the hash is achieved, when rehash

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.