Several hashmaps in Java

Source: Internet
Author: User
1. hashmap, that is, java. util. hashmap

Implementation of the standard link address Method. This does not need to be parsed and is very clear. (Picture from network)

Ii. Thread-safe hashmap returned by the collections. synchronizedmap () function

This implementation is relatively simple.

CodeIncluding:

 
Private final map <K, V> m; // backing map final object mutex; // object on which to synchronize

Synchronized (mutex) is added to all methods ).

However, this hashmap cannot be iterated at will, because it simply encapsulates hashmap. Looking back at the implementation of hashmap, we can find that a linked list is formed for conflicting keys, obviously, if one thread is experiencing hashmap and the other is performing the delete operation, an error may occur.

Therefore, the following code is provided in JDK:

 
Map M = collections. synchronizedmap (New hashmap ());... set S = m. keyset (); // needn't be in synchronized block... synchronized (m) {// synchronizing on M, not s! Iterator I = S. iterator (); // must be in synchronized block While (I. hasnext () Foo (I. Next ());}
 
 

3. concurrenthashmap

For hashmap, the most important operations are as follows:

 
Public v get (Object key) Public v put (K key, V value) Public v remove (Object key) iteration

In a multi-threaded environment, get, put, and remove are all relatively easy to implement (if efficiency is not taken into account, simply apply the lock). Iterative operations are the real difficulties.

From the perspective of collections. synchronizedmap () iteration, it cannot be transparent to customer code, which is a little tricky.

The following is a simple analysis of the concurrenthashmap implementation, which is quite delicate.

By default, a concurrenthashmap contains 16ChildHashmapSo it is equivalent to a second-level hash. All operations are first located to the sub-hashmap and then performed accordingly.

For:

Public v get (Object key)

First obtain the table where the key is located, and then get it like hashmap.
No locks in the middle

Public v put (K key, V value)

First obtain the table to which it belongs and lock it.
Determine whether a table needs to be resized
If the table is to be resized, newtable is generated.
Moving slots with the same hash value to newtable
The slots with different hash values copy all entries in the oldtable to newtable.
Because other threads may experience this table, you cannot split the original linked list.

Public v remove (Object key)

Remove operation. For example, to delete entry3, set entry1 to entry1 *, entry1 * To entry4, entry2 to entry2 *, and entry2 * To entry1 *, A two-way linked list is formed. The original entry1, entry2, and entry3 will be automatically recycled by GC.

Iterative operation:

The calendar times of concurrenthashmap are passed from the back to the forward, because if another thread B executes the clear operation, it sets all slots in the table to null, this operation is performed from front to back.
If thread a is back-to-back when it goes through the map, it may catch up.

Run the following code:

Hashmap <integer, string> m1 = new hashmap (); m1.put (1, "001"); m1.put (2, "002"); For (Entry <integer, string> entry: m1.entryset () {system. out. println ("key:" + entry. getkey ());}

Hashmap outputs key: 1 key: 2
Concurrenthashmap outputs key: 2 key: 1


TotalConclusion: there are too many class libraries in Java, and there are still many hashmap classes. You can add them when you are free.

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.