Hash-2.hashmap

Source: Internet
Author: User

Data structure of 1.HASHMAP
A.hashmap is a combination of a list hash, that is, the combination of an array and a linked list.
An array of type Entry is defined in the B.hashmap class, Entry [], Entry has a key value hash next property
such as code

transient entry[] table;      Static class Implements Map.entry<k,v> {      final  K key;      V value;      Entry<K,V> next;       Final int Hash;      ...  }  

C. Structure diagram

The bottom of the d.hashmap is an array structure, and each item in the array is a linked list. When a new HashMap is created,
An array is initialized. The elements of the table array are of type entry.
Each entry element is actually a key-value pair, and it holds a reference to the next entry element
, which means that each entry element of the table array also acts as the first node of a entry linked list.
Points to the next entry element of the list, which is called a "chain-table hash" data structure, which is the combination of arrays and linked lists.

The 2.HASHMAP storage implementation
When we put the element in the HashMap, we start with the hashcode of the key's recalculated element,
According to Hashcode, the position of this element in the table array (that is, subscript), if the array is already stored in the position of other elements, then the elements in this position will be stored in the form of a list, the new addition placed in the chain head,
The first to join is at the end of the chain. If the array has no elements at that position, the element is placed directly at that position in the array.
Code:

 Publicv put (K key, V value) {//The hashmap allows null keys and null values to be stored. //when key is null, the Putfornullkey method is called, and value is placed in the first position of the array.    if(Key = =NULL)         returnPutfornullkey (value); //The hash value is recalculated based on the keycode of the key.    inthash =Hash (Key.hashcode ()); //searches for the index of the specified hash value in the corresponding table.    inti =indexfor (hash, table.length); //if the Entry at the I index is not NULL, the next element of the E element is traversed continuously through the loop.     for(entry<k,v> e = table[i]; E! =NULL; E =e.next) {Object k; //If a entry hash of the linked list at the I index is found to be equal to the hash of the new entry and the key is the same, the new entry overwrites the old entry and returns.        if(E.hash = = Hash && (k = e.key) = = Key | |Key.equals (k))) {V OldValue=E.value; E.value=value; E.recordaccess ( This); returnOldValue; }     }     //If the entry at the I index is null, there is no entry here. modcount++; //adds the key, value, to the I index. addentry (hash, key, value, I); return NULL;

3. Reading elements
When you get an element from HashMap, first calculate the hashcode of the key and find an element in the corresponding position in the array.
The required element is then found in the linked list of the corresponding position through the Equals method of key.

 PublicV get (Object key) {if(Key = =NULL)          returnGetfornullkey (); inthash =Hash (Key.hashcode ());  for(Entry<k,v> e =table[indexfor (hash, table.length)]; E!=NULL; E=e.next) {Object k; if(E.hash = = Hash && (k = e.key) = = Key | |Key.equals (k))) returnE.value; }      return NULL; }  

4. To sum up simply, HashMap at the bottom of the key-value as a whole to deal with,
This whole is a Entry object. The HashMap bottom uses a entry[] array to hold all key-value pairs,
When a Entry object needs to be stored, the hash algorithm is used to determine where it is stored in the array.
The storage location in the linked list on the array location is determined by the Equals method.

Hash-2.hashmap

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.