Java Collections Framework - ConcurrentHashMap class

Java Collections Framework - ConcurrentHashMap class


Posted in : Core Java Posted on : May 19, 2011 at 3:25 PM Comments : [ 0 ]

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 :

  1. put()
  2.                         syntax : public V put(K key,V value)

  3. clear()
  4.                         syntax : public void clear()

  5. elements()
  6.                         syntax : public Enumeration elements()

  7. remove(Object key, Object value)
  8.                         syntax : public boolean remove(Object key,Object value)

  9. replace(K key, V value)
  10.                         syntax : public V replace(K key,V value)

  11. values()
  12.                         syntax : public Collection values()

  13. size()
  14.                         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]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics