The storage structure and principle of HashMap, and the storage structure of hashmap

Source: Internet
Author: User

The storage structure and principle of HashMap, and the storage structure of hashmap

1. HashMap data structure (HashMap uses hashcode to quickly find its content, which is out of order)

There are arrays and linked lists in the data structure to store data, but these two are basically two extremes.

Array: The Storage Area of the array is continuous and memory usage is serious, so the space is very complex. However, the time for Binary Search of arrays is small. array features: Easy addressing, insertion, and

Difficult to delete.

Linked List: The Storage Area of the linked list is discrete, and the memory usage is relatively loose, so the space complexity is very small, but the time complexity is large. Features of the linked list: difficult addressing, easy insertion and deletion.

Hash table

HashMap is composed of arrays and linked lists. It is easy to address and insert or delete. (Storage unit array Entry [], which contains a linked list)

HashMap is actually implemented by a linear array. Therefore, it can be understood that the container for storing data is a linear container;

In HashMap, there is an internal static class Entry. Its important attributes include key, value, and next. From the attribute key and value, it is obvious that the Entry is

A basic bean implemented by the HashMap key-value pair; that is to say, the basis of HashMap is a linear array, and the array is Entry []. The content in the Map is saved.

In Entry;

    /**     * The table, resized as necessary. Length MUST Always be a power of two.     */    transient Entry[] table;

2. Access Implementation of HashMap

2.1: Storage

Here, HashMap uses an algorithm.

// Storage:

Int hash = key. hashCode ();// Obtain the hashCode Of the key. The value is a fixed int value.

Int index = hash % Entry []. length;// Obtain the array Subscript: the hash value of the key is used to obtain the remainder of the Entry array length.

Entry [index] = value;

Note: If the two keys have the same index through hash % Entry []. length, will they be overwritten?

No. The Entry class has a next attribute to point to the next Entry. For example, the first key-value pair A is obtained by calculating its key hash.

Index = 0, record as: Entry [0] =. After a while, another key-Value Pair B will be added. Its index is equal to 0 after calculation. What should I do now? HashMap will do this: B. next =

A, Entry [0] = B. If C is introduced, and index is equal to 0, C. next = B, Entry [0] = C; in this way, we find that index = 0 actually accesses three key-value pairs A, B, and C.

Are linked together through the next attribute. So don't worry.

That is to say, the Entry [] array stores the last inserted data.

Public V put (K key, V value) {if (key = null) return putForNullKey (value ); // null is always placed in the first linked list of the array. int hash = hash (key. hashCode (); int I = indexFor (hash, table. length); // time series table for (Entry <K, V> e = table [I]; e! = Null; e = e. next) {Object k; // if the key already exists in the linked list, replace it with the new value 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;} void addEntry (int hash, K key, V value, int bucketIndex) {Entry <K, V> e = table [bucketIndex]; table [bucketIndex] = new Entry <K, V> (hash, key, Value, e); // parameter e, which is Entry. next // If the size exceeds threshold, the table size is expanded. Then hash if (size ++> = threshold) resize (2 * table. length );}

2.2: Value

Obtain the hashcode of a key. obtain Entry [hash % Entry []. length] using the hash value, locate the array element, and traverse the element.

The linked list at the prime location.

// When the value is set:

Int hash = key. hashCode ();

Int index = hash % Entry []. length;

Return Entry [index];

Public V get (Object key) {if (key = null) return getForNullKey (); int hash = hash (key. hashCode (); // first locate the array element, and then traverse the linked list at the element 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) return e. value;} return null ;}

When the capacity of the hash table exceeds the default capacity, you must adjust the table size. When the capacity reaches the maximum value, the method Integer. MAX_VALUE is returned. In this case, you need to create

A table maps the original table to the new table.

3. thread security issues of HashMap, HashTable, and ConcurrentHashMap

HashMap: The thread is insecure.
HashTable:Lock the entire hash table to exclusively occupy the thread. HashMap can be empty. By analyzing Hashtable, we can see that synchronized is for the entire Hash table, that is, the entire table is locked each time.

There is a huge waste behind the security of exclusive threads.

ConcurrentHashMap: a faster hashmap that provides much better concurrency. Multiple read operations are almost always executed concurrently.It is the lock segment (default:Hash Tables are divided into 16

Segment) In get, put, remove, and other operations, ConcurrentHashMap only locks the currently used segments and only locks the entire hash table when the size is obtained.

Related Article

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: 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.