Implementation principle of hashtable and hashmap in JVM, hashtablehashmap

Source: Internet
Author: User

Implementation principle of hashtable and hashmap in JVM, hashtablehashmap
Implementation Principles of hashtable and hashmap in JVMArticle category: Java programming

Reprinted

 

In hashtable and hashmap areJavaThe common container classes,

Is the class under the Java. uitl package,

So how does Hashtable and Hashmap pair hash key-value pairs? Let's take a look atSource codeTo analyze the Hashtable constructor. put (K, V) joins the method and get (Object) method to get it.

1. Hashtable Construction Method: Hashtable (int initialCapacity, float loadFactor)

Public Hashtable (int initialCapacity, float loadFactor ){
If (initialCapacity <0)
Throw new IllegalArgumentException ("Illegal Capacity:" +
InitialCapacity );
If (loadFactor <= 0 | Float. isNaN (loadFactor ))
Throw new IllegalArgumentException ("Illegal Load:" + loadFactor );

If (initialCapacity = 0)
InitialCapacity = 1;
This. loadFactor = loadFactor;
Table = new Entry [initialCapacity];
Threshold = (int) (initialCapacity * loadFactor );
}

There is something to say inside the rice. Note that one table is an object array, and the input initialCapacity indicates the size of this array, and the threshold is an int value, where loadFactor is 0.75 by default, it means that when the value in the table reaches the threshold of 75%, it will automatically double the number capacity when it is about to fill the array.

The Entry <K, V> comes from

Private static class Entry <K, V> implements Map. Entry <K, V> {
Int hash;
K key;
V value;
Entry <K, V> next;
Is a single linked list,

Is the Hashtable table,

Ii. Add put (K, V)

Public synchronized V put (K key, V value ){
// Make sure the value is not null
If (value = null ){
Throw new NullPointerException ();
}

// Makes sure the key is not already in the hashtable.
Entry tab [] = table;
Int hash = key. hashCode ();
Int index = (hash & 0x7FFFFFFF) % tab. length;
For (Entry <K, V> e = tab [index]; e! = Null; e = e. next ){
If (e. hash = hash) & e. key. equals (key )){
V ld = e. value;
E. value = value;
Return old;
}
}

ModCount ++;
If (count> = threshold ){
// Rehash the table if the threshold is exceeded
Rehash ();

Tab = table;
Index = (hash & 0x7FFFFFFF) % tab. length;
}

// Creates the new entry.
Entry <K, V> e = tab [index];
Tab [index] = new Entry <K, V> (hash, key, value, e );
Count ++;
Return null;
}

This takes a long look. You only need to pay attention to three points,

1. index. index is the hashcode of the key value and the & Operation of 0x7fffff. Then, the remainder value is obtained based on the array length. In this way, you can determine the name of the queue,

2,

For (Entry <K, V> e = tab [index]; e! = Null; e = e. next ){
If (e. hash = hash) & e. key. equals (key )){
V ld = e. value;
E. value = value;
Return old;
}
}

In the for statement, e is used to traverse a queue named index single linked list in the tab. The definitions of the first single linked list are defined by index to see if the queue already has the same value. If yes, returns this value.

3. If not, add

Entry <K, V> e = tab [index];
Tab [index] = new Entry <K, V> (hash, key, value, e );

3. get (Object)

Public synchronized V get (Object key ){
Entry tab [] = table;
Int hash = key. hashCode ();
Int index = (hash & 0x7FFFFFFF) % tab. length;
For (Entry <K, V> e = tab [index]; e! = Null; e = e. next ){
If (e. hash = hash) & e. key. equals (key )){
Return e. value;
}
}
Return null;
}

This is a good understanding of int index = (hash & 0x7FFFFFFF) % tab. length;

Get queue name,

For (Entry <K, V> e = tab [index]; e! = Null; e = e. next ){
If (e. hash = hash) & e. key. equals (key )){
Return e. value;
}
}

Traverse the team and obtain the specific value based on the hash value.

 

In summary, one is to determine the queue based on the index and obtain the specific element value from the hash value.

 

After reading hashtable, let's take a look at hashMap.
HashMap can be regarded as an upgraded version of hashtable, which was first available since 1.2.

However, there are two main differences between the two.
1. The read and write operations of hashMap are unsynchronized. Be sure to use it in a multi-threaded environment.
Hashtable is synchronized.
The difference between the two is achieved by adding the synchronized keyword to the read/write method.

The second difference is that hashMap can empty values, and hashtable will report an error.

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.