Java Program to Implement ConcurrentHashMap API

This Java program is to Implement ConcurrentHashMap API.A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates. This class obeys the same functional specification as Hashtable, and includes versions of methods corresponding to each method of Hashtable. However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access.

Here is the source code of the Java program to Implement ConcurrentHashMap API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.Enumeration;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Set;
  8. import java.util.concurrent.ConcurrentHashMap;
  9.  
  10. public class ConcurrentHashMapImpl<K, V>
  11. {
  12.     private ConcurrentHashMap<K, V> concurrentHashMap;
  13.  
  14.     /**
  15.      * Constructs an empty insertion-ordered LinkedHashMap instance with the
  16.      * default initial capacity (16) and load factor (0.75).
  17.      **/
  18.     public ConcurrentHashMapImpl()
  19.     {
  20.         concurrentHashMap = new ConcurrentHashMap<K, V>();
  21.     }
  22.  
  23.     /**
  24.      * Constructs an empty insertion-ordered LinkedHashMap instance with the
  25.      * specified initial capacity and a default load factor (0.75).
  26.      **/
  27.     public ConcurrentHashMapImpl(int initialCapacity)
  28.     {
  29.         concurrentHashMap = new ConcurrentHashMap<K, V>(initialCapacity);
  30.     }
  31.  
  32.     /**
  33.      * Constructs an empty insertion-ordered LinkedHashMap instance with the
  34.      * specified initial capacity and load factor.
  35.      **/
  36.     public ConcurrentHashMapImpl(int initialCapacity, float loadFactor)
  37.     {
  38.         concurrentHashMap = new ConcurrentHashMap<K, V>(initialCapacity, loadFactor);
  39.     }
  40.  
  41.     /**
  42.      * Constructs an empty LinkedHashMap instance with the specified initial
  43.      * capacity, load factor and ordering mode.
  44.      **/
  45.     public ConcurrentHashMapImpl(int initialCapacity, float loadFactor, int concurrencyLevel)
  46.     {
  47.         concurrentHashMap = new ConcurrentHashMap<K, V>(initialCapacity, loadFactor, concurrencyLevel);
  48.     }
  49.  
  50.     /**
  51.      * Constructs an insertion-ordered LinkedHashMap instance with the same
  52.      * mappings as the specified map.
  53.      **/
  54.     public ConcurrentHashMapImpl(Map<? extends K, ? extends V> m)
  55.     {
  56.         concurrentHashMap = new ConcurrentHashMap<K, V>(m);
  57.     }
  58.  
  59.     /** Removes all of the mappings from this map. **/
  60.     public void clear()
  61.     {
  62.         concurrentHashMap.clear();
  63.     }
  64.  
  65.     /** Returns true if this map contains a mapping for the specified key. **/
  66.     public boolean containsKey(Object key)
  67.     {
  68.         return concurrentHashMap.containsKey(key);
  69.     }
  70.  
  71.     /** Returns true if this map maps one or more keys to the specified value. **/
  72.     public boolean containsValue(Object value)
  73.     {
  74.         return concurrentHashMap.containsValue(value);
  75.     }
  76.  
  77.     /** Returns a Set view of the mappings contained in this map. **/
  78.     public Set<Map.Entry<K, V>> entrySet()
  79.     {
  80.         return concurrentHashMap.entrySet();
  81.     }
  82.  
  83.     /**
  84.      * Returns the value to which the specified key is mapped, or null if this
  85.      * map contains no mapping for the key.
  86.      **/
  87.     public V get(Object key)
  88.     {
  89.         return concurrentHashMap.get(key);
  90.     }
  91.  
  92.     /** Returns true if this map contains no key-value mappings. **/
  93.     public boolean isEmpty()
  94.     {
  95.         return concurrentHashMap.isEmpty();
  96.     }
  97.  
  98.     /** Returns a Set view of the keys contained in this map. **/
  99.     public Set<K> keySet()
  100.     {
  101.         return concurrentHashMap.keySet();
  102.     }
  103.  
  104.     /** Associates the specified value with the specified key in this map. **/
  105.     public V put(K key, V value)
  106.     {
  107.         return concurrentHashMap.put(key, value);
  108.     }
  109.  
  110.     /** Copies all of the mappings from the specified map to this map. **/
  111.     public void putAll(Map<? extends K, ? extends V> m)
  112.     {
  113.         concurrentHashMap.putAll(m);
  114.     }
  115.  
  116.     /** Removes the mapping for the specified key from this map if present. **/
  117.     public V remove(Object key)
  118.     {
  119.         return concurrentHashMap.remove(key);
  120.     }
  121.  
  122.     /** Returns the number of key-value mappings in this map. **/
  123.     public int size()
  124.     {
  125.         return concurrentHashMap.size();
  126.     }
  127.  
  128.     /** Returns a Collection view of the values contained in this map. **/
  129.     public Collection<V> values()
  130.     {
  131.         return concurrentHashMap.values();
  132.     }
  133.  
  134.     /** Returns an enumeration of the values in this table. **/
  135.     public Enumeration<V> elements()
  136.     {
  137.         return concurrentHashMap.elements();
  138.     }
  139.  
  140.     /**
  141.      * If the specified key is not already associated with a value, associate it
  142.      * with the given value.
  143.      **/
  144.     public V putIfAbsent(K key, V value)
  145.     {
  146.         return concurrentHashMap.putIfAbsent(key, value);
  147.     }
  148.  
  149.     /** Replaces the entry for a key only if currently mapped to some value. **/
  150.     public V replace(K key, V value)
  151.     {
  152.         return concurrentHashMap.replace(key, value);
  153.     }
  154.  
  155.     /** Replaces the entry for a key only if currently mapped to a given value. **/
  156.     public boolean replace(K key, V oldValue, V newValue) 
  157.     {
  158.         return concurrentHashMap.replace(key, oldValue, newValue);
  159.     }
  160.  
  161.     public static void main(String... arg)
  162.     {
  163.         ConcurrentHashMapImpl<Integer, Integer> concurrentHashMap 
  164.                     = new ConcurrentHashMapImpl<Integer, Integer>();
  165.         concurrentHashMap.put(1, 100);
  166.         concurrentHashMap.put(2, 200);
  167.         concurrentHashMap.put(3, 300);
  168.         concurrentHashMap.put(4, 400);
  169.  
  170.         Map<Integer, Integer> anotherMap = new HashMap<Integer, Integer>();
  171.         concurrentHashMap.putAll(anotherMap);
  172.  
  173.         System.out.println("the key set of the concurrentHashMap is ");
  174.         Set<Integer> keySet = concurrentHashMap.keySet();
  175.         Iterator<Integer> itr = keySet.iterator();
  176.         while (itr.hasNext())
  177.         { 
  178.             System.out.print(itr.next() + "\t");
  179.         }
  180.         System.out.println();
  181.         System.out.println("the values of the concurrentHashMap is ");
  182.         Collection<Integer> collectionValues = concurrentHashMap.values();
  183.         itr = collectionValues.iterator();
  184.         while (itr.hasNext())
  185.         {
  186.             System.out.print(itr.next() + "\t");
  187.         }
  188.         System.out.println();
  189.         System.out.println("the entry set of the concurrentHashMap is ");
  190.         Iterator<Entry<Integer, Integer>> eitr;
  191.         Set<Entry<Integer, Integer>> entrySet = concurrentHashMap.entrySet();
  192.         eitr = entrySet.iterator();
  193.         while (eitr.hasNext())
  194.         {
  195.             System.out.println(eitr.next() + "\t");
  196.         }
  197.         System.out.println("the concurrentHashMap contains Key 3 :" + concurrentHashMap.containsKey(3));
  198.         System.out.println("the concurrentHashMap contains Value 600 :"
  199.                +courrentHashMap.containsValue(600));
  200.         System.out.println("Put the key 10 with value 1000  if not asscociated : "
  201.                + concurrentHashMap.putIfAbsent(10, 1000));
  202.         System.out.println("replace key 3 oldvalue of 300 and newvalue 500 :"
  203.                + concurrentHashMap.replace(3, 300, 500));
  204.         System.out.println("the size of the concurrentHashMap is "
  205.                + concurrentHashMap.size());
  206.         concurrentHashMap.clear();
  207.         if (concurrentHashMap.isEmpty())
  208.             System.out.println("the concurrentHashMap is empty");
  209.         else
  210.             System.out.println("the concurrentHashMap is not empty");
  211.     }
  212. }

$ javac  ConcurrentHashMapImpl.java
$ java ConcurrentHashMapImpl
the key set of the concurrentHashMap is 
2	1	3	4	
the values of the concurrentHashMap is 
200	100	300	400	
the entry set of the concurrentHashMap is 
2=200	
1=100	
3=300	
4=400	
the concurrentHashMap contains Key 3 :true
the concurrentHashMap contains Value 600 :false
Put the key 10 with value 1000  if not asscociated : null
replace key 3 oldvalue of 300 and newvalue 500 :true
the size of the concurrentHashMap is 5
the concurrentHashMap is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.