ConcurrentHashMap extends the abstract class AbstractMap and implements the ConcurrentMap interface. This class follows the operational specification and the similar functional specification as of Hashtable, for updating a hash table supports the fully concurrency of the recoveries and adjustable expected concurrency. This class allows the variants of methods correspondence to each method of Hashtable and also doesn't permit the 'null' for the 'key' or 'value'. All the operations of this class are thread-safe. Use of ConcurrentHashMap increases the performance because it permits the multiple threads to modify the map concurrently without require to block them however performance may be poor if the single threads access the map at a time.
Java Collections Framework - ConcurrentHashMap class
ConcurrentHashMap extends the abstract class AbstractMap and implements the ConcurrentMap interface. This class follows the operational specification and the similar functional specification as of Hashtable, for updating a hash table supports the fully concurrency of the recoveries and adjustable expected concurrency. This class allows the variants of methods correspondence to each method of Hashtable and also doesn't permit the 'null' for the 'key' or 'value'. All the operations of this class are thread-safe. Use of ConcurrentHashMap increases the performance because it permits the multiple threads to modify the map concurrently without require to block them however performance may be poor if the single threads access the map at a time.
Syntax
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>
Constructor's of ConcurrentHashMap
This class provides constructor through which we can create map according to our requirement :
ConcurrentHashMap() : This constructor makes a new vacate map of default size (16), default load factor (0.75) and concurrencyLevel (16).
ConcurrentHashMap(int initialCapacity) : This constructor makes a new vacate map of capacity defined at time of instantiation according to requirement with default load factor (0.75) and concurrencyLevel (16).
ConcurrentHashMap(int initialCapacity, float loadFactor) : This constructor makes a new vacate map of capacity and load factor defined at time of instantiation according to requirement and with default concurrencyLevel (16).
ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) : This constructor makes a new vacate map of capacity, load factor, and with concurrencyLevel defined at the time of instantiation according to requirement.
ConcurrentHashMap(Map<? extends K,? extends V> m) : This constructor makes the similar mappings according to the map given.
Methods of ConcurrentHashMap
This class provides methods some of the commonly used are :
- put()
- clear()
- elements()
- remove(Object key, Object value)
- replace(K key, V value)
- values()
- size()
syntax : public V put(K key,V value)
syntax : public void clear()
syntax : public Enumeration
syntax : public boolean remove(Object key,Object value)
syntax : public V replace(K key,V value)
syntax : public Collection
syntax : public int size()
Example :
package devmanuals.com; import java.util.concurrent.ConcurrentHashMap; import java.util.Set; public class ConcurrentHashMapDemo { enum Days{ Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday } public static void main(String args[]){ ConcurrentHashMap chm = new ConcurrentHashMap(); chm.put(1, "A"); chm.put(2, "B"); chm.put(3, "C"); chm.put(4, "D"); chm.put(5, "E"); System.out.println("Mappings are : "+chm); Set s = chm.entrySet(); System.out.println("Set view of mappings = "+s); Set s1 = chm.keySet(); System.out.println("Set view of keys = "+s1); } }
Output :
Mappings are : {5=E, 4=D, 3=C, 2=B, 1=A} Set view of mappings = [5=E, 4=D, 3=C, 2=B, 1=A] Set view of keys = [5, 4, 3, 2, 1] |
[ 0 ] Comments