Implementation of Hashtable and HashMap inside the JVM

Source: Internet
Author: User
Tags rehash

article Category: Java programming

Reproduced

The hashtable and HashMap are common container classes in Java ,

is the class under the JAVA.UITL package,

Then Hashtable and HashMap is how to achieve hash key pair pairing, we look at the JDK inside the source code , analysis under the Hashtable method of construction, put (K, V) Add method and get (Object) method is probably understand.

First, the construction method of Hashtable: 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);
}

What to say in this inside meter, note that table one is an entity array, the input initialcapacity represents the size of the array, and threshold is an int value, Loadfactor default is 0.75, that is, when the value of the table inside the 75% threshold, is about to fill the array, will automatically double the expansion of digital capacity.

and entry<k,v> from the

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 Hashtable's Watch,

Second, put (K, V) Join method

Public synchronized v put (K key, V value) {
Make sure the value was 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 looks very long, just pay attention to three o'clock,

1.index,index is the & of the hashcode and 0x7fffffff of the key value, and then the remainder value is calculated based on the length of the array. This allows you to set the name of your 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;
}
}

The For statement inside is to let E in the tab a queue name is the index chain inside the loop, the definition of the list of individual lists by index definition, see if the queue already has the same value, if any, return the value.

3, if not, then join

Entry<k,v> e = Tab[index];
Tab[index] = new entry<k,v> (hash, key, value, E);

Third, get (Object), get

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;
}

That's a good idea. int index = (hash & 0x7FFFFFFF)% Tab.length;

Get the 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 get a specific value based on the hash value.

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

After reading the Hashtable, we're looking at HashMap.
HashMap can be counted as an upgraded version of Hashtable, with the earliest starting from 1.2.

However, the main difference between the two is two points.
1. HashMap Read and write is unsynchronized, in the multi-threaded environment to pay attention to the use of
and Hashtable is synchronized.
The difference between the two is achieved by adding synchronized keywords to the read-write method.

The second difference is that HashMap can empty the value, and Hashtable will be an error.

Implementation of Hashtable and HashMap inside the JVM

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.