Java HashMap and javahashmap

Source: Internet
Author: User

Java HashMap and javahashmap

Hash image, which is often used in Java and mainly stores key-value data. HashMap implements the Map interface. The system uses the Hash algorithm to calculate the location where key-value is stored, so that the key-value pair of Map can be quickly accessed.

Storage Implementation of HashMap

HashMap uses a so-called "Hash algorithm" to determine the storage location of each element. The following uses the source code to parse the implementation of HashMap.

HashMap <String, String> map = new HashMap <String, String> ();
Map. put ("A", "Forsta ");
Map. put ("B", "Sam ");
Map. put ("C", "Jack ");

HashMap saves key-value to hashmap using the put method.

 1  public V put(K key, V value) { 2         if (key == null) 3             return putForNullKey(value); 4         int hash = hash(key.hashCode()); 5         int i = indexFor(hash, table.length); 6         for (Entry<K,V> e = table[i]; e != null; e = e.next) { 7             Object k; 8             if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { 9                 V oldValue = e.value;10                 e.value = value;11                 e.recordAccess(this);12                 return oldValue;13             }14         }15 16         modCount++;17         addEntry(hash, key, value, i);18         return null;19     }

The specific process for saving key-value is to first obtain the key and determine whether the key is null. If it is null, the value is directly saved to key = null. If the key keyword is already stored in the Hash table, it overwrites the current location. If it is not saved, add an Entry and insert it into the table. The newly added Entry will form an Entry chain with the original Entry in the set, and the newly added Entry is located in the header of the Entry chain.

Otherwise, calculate the hash value of the key to obtain the hashCode of the key object --- Hash algorithm --- to obtain the table index value.

1  static int hash(int h) {2         h ^= (h >>> 20) ^ (h >>> 12);3         return h ^ (h >>> 7) ^ (h >>> 4);4     }
1  static int indexFor(int h, int length) {2         return h & (length-1);3     }

This method is very clever. It always uses h & (table. length-1) to obtain the storage location of the object. Table. length-1 ensures that the returned index is smaller than the table length and must be in the table.

1 void addEntry(int hash, K key, V value, int bucketIndex) {2         Entry<K,V> e = table[bucketIndex];3         table[bucketIndex] = new Entry<>(hash, key, value, e);4         if (size++ >= threshold)5             resize(2 * table.length);6     }

The addEntry method puts the newly inserted Entry into the array table. If there is already Entry data in the previous table, the newly added Entry object next is directed to the original object to form the Entry chain. If not, it points to null.

If the data exceeds the threshold value, it is resized, but the original size is twice each time.

The specific structure of HashMap is as follows:

Read Implementation of HashMap

When the Entry stored in each bucket of HashMap is only a single Entry -- that is, the Entry chain is not generated through the pointer, HashMap has the best performance: when the program extracts the corresponding value through the key, the system only needs to calculate the return value of the hashCode () of the key, find the index of the key in the table Array Based on the return value of the hashCode, and then retrieve the Entry at the index, finally, return the value corresponding to the key. Check the get (K key) method code of the HashMap class:

 1  public V get(Object key) { 2         if (key == null) 3             return getForNullKey(); 4         int hash = hash(key.hashCode()); 5         for (Entry<K,V> e = table[indexFor(hash, table.length)]; 6              e != null; 7              e = e.next) { 8             Object k; 9             if (e.hash == hash && ((k = e.key) == key || key.equals(k)))10                 return e.value;11         }12         return null;13     }

 


Java technology: What are the differences between Vector, HashMap, and ArrayList? What is better?

Generally, ArrayList arrays are commonly used. vector is a Vector Array. Compared with ArrayList, the Vector thread is secure, that is, synchronous. hashMap is a Map set that stores key-value pairs.

What is the use of the HashMap class in java?

HashMap is a set of key-value pairs in the form of key = value, where key is unique and value can be repeated. If you need this form in development, you can use 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.