HashMap and Hashtable in Java

Source: Internet
Author: User

For map, the most intuitive is understanding is the key value pair, mapping, Key-value form. A map cannot contain duplicate keys, and a key can have only one value. Usually when we use, the most commonly used is nothing more than HashMap.

The HASHMAP implements the map interface, allows NULL values and NULL keys, and does not guarantee the mapping order.

HASHMAP has two parameters that affect performance:

Initial capacity: Represents a scale in which a hash table can reach a full amount before its capacity increases automatically
Load factor: When an entry in a hash table exceeds the product of the capacity and load factor, a re-hash is done.
The following member variable source code:

1 Static Final float Default_load_factor = 0.75f; 2 Static Final int default_initial_capacity = 1 << 4; 3 transient Node<k,v>[] table;

As you can see, the default load factor is 0.75, and the default capacity is 1 << 4, which is 16. Loading factor is too high, prone to hash conflict, loading factor is too small, easy to waste space, 0.75 is a compromise.

In addition, the entire HASHMAP implementation principle can be easily understood as: when we put the time, we first calculate a value x, and then in Table[x] to store our values. One benefit is that the time complexity of subsequent get operations is directly O (1), since HASHMAP is an array-based implementation.

Hash (key) This means that a value is calculated based on the key value to determine which position of the table array to hold our values. (Ps: This hash (key) method is also very fastidious, will seriously affect performance, the implementation of the HashMap O (1) time complexity will be reduced to O (n), in the JDK8 below the version of the catastrophic impact. It needs to ensure that the resulting number is evenly distributed in the hash table in order to reduce hash collisions)

Important NOTE:

JDK8 in the conflict too much, the list will turn red black tree, Time complexity is O (LOGN), will not be O (n)
JDK8 in the conflict too much, the list will turn red black tree, Time complexity is O (LOGN), will not be O (n)
JDK8 in the conflict too much, the list will turn red black tree, Time complexity is O (LOGN), will not be O (n)
Then, we see:

1 if NULL )2     null); 3 Else {4...     

This means that if there is no hash conflict, then you can put the data tab[i] = NewNode (hash, key, value, NULL); If there is a hash conflict, then executing else needs to resolve the SID conflict.

Then the data is actually set up a node, the node has the attribute Key,value, save our key value and value values, and then put the node into the table array, and there is no mysterious place.

1 Static classNode<k,v>ImplementsMap.entry<k,v> {2     Final intHash;3     FinalK key;4 V value;5Node<k,v>Next;6 7Node (intHash, K key, V value, node<k,v>next) {8          This. hash =Hash;9          This. Key =key;Ten          This. Value =value; One          This. Next =Next; A     } -}

As you can see, there is a node<k,v> next in node nodes. , in fact, we should know that this is to solve the conflict between the HA and the Greek. Let's take a look at how to resolve the conflict in Kazakhstan:

Hash conflict: In layman's words, we start with a put operation that calculates the value we want to put in the X position of the table array. So the next time we do a put operation, we calculate that we want to put this value in the X position of the table array, which has already been placed in the value, then how to deal with it now?

In fact, it is solved by the link list method.

First, if there is a hash conflict, then:

1 if (P.hash = = Hash &&2     null && key.equals (k))))3 e = p;

You need to determine if the key is the same, because HASHMAP cannot add duplicate keys. If the same, then overwrite, if not the same, then first determine whether it is TreeNode type:

else if (p instanceof TreeNode)    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);

Here is not now turned red black tree (in the case of a large number of hash conflicts, the list will turn red black tree), generally our small data in the case, is not to turn, so here for the moment not to consider this situation (Ps: I also did not go into the red black tree, so do not say this).

If it is normal, the following statement is executed to resolve the hash conflict:

1  for(intBincount = 0;; ++Bincount) {2     if((e = p.next) = =NULL) {3P.next = NewNode (hash, key, value,NULL);4         if(Bincount >= treeify_threshold-1)//-1 for 1st5 treeifybin (tab, hash);6          Break;7     }8     if(E.hash = = Hash &&9(k = e.key) = = Key | | (Key! =NULL&&key.equals (k) )))Ten          Break; Onep =e; A}

In fact, this is the use of linked list method to solve. And:

The conflicting nodes are placed at the bottom of the list.
The conflicting nodes are placed at the bottom of the list.
The conflicting nodes are placed at the bottom of the list.

Because first there is: p = tab[i = (n-1) & Hash], then for loop, then there is if ((e = p.next) = = null) {, and if the next node of the current node has a value, then P = e; Surface.

HashMap and Hashtable in Java

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