Latest Code Tutorials

ConcurrentHashMap in Java Example | Java ConcurrentHashMap


Java ConcurrentHashMap is done in JDK 1.5, which allows both concurrent maps as well as serialization interface. It is the modification of HashMap. It is a hash table supporting full concurrency of retrievals and high expected concurrency for updates.

ConcurrentHashMap in Java

Most of the operations are safe for threading, retrieval operations do not accept locking, and there is no support for locking the entire table in a way that prevents all access.


public class ConcurrentHashMap<K,V>  
extends AbstractMap<K,V>  
implements ConcurrentMap<K,V>, Serializable  

#List of ConcurrentHashMap class Methods

1.void clear() The clear() helps to remove all the mapping from this map in concurrentHashMap.
2.Compute (K key, Bifunction<? super K,? super V>remappingFuctionThe compute( ) method of the ConcurrentHashMap class is used to calculate the required mapping for the specified key and its recent mapped value.
3.computeIfAbsent(K key, Function<? Super K,? extends V> mappingFunction)The computeIfAbsent ()  method of ConcurrentHashMap class attempts to compute its value from the accessible mapping function and injects it into this map until null occurs.
4.computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)The computeIfPresent() method of ConcurrentHashMap class Attempts to compute a new mapping given the key and its current mapped value, If the value for the specified key is present.
5.boolean contains(Object value)The contains() method of ConcurrentHashMap class tests of some keymaps into the specified value.
6.boolean containsKey (Object key)The containsKey() method of ConcurrentHashMap class tests works if the mentioned object is a key in the table.
7.boolean containsValue(Object value)The containsValue() method of ConcurrentHashMap class returns true if this map maps one or more keys to the specified value. This method may require a full traversal of the map and is much slower than the method containsKey.
8.public Enumeration<V> elements()The elements() method of ConcurrentHashMap class returns an enumeration of the values in this table.
9.Set<Map.Entry<K,V>> entrySet()The entrySet() method of ConcurrentHashMap class returns a Set view of the mappings in this map. The modifications made to the map are reflected in the set, and so on.
10. boolean equals(Object o)The elements() method of ConcurrentHashMap class Compares the mentioned object with this map for equality and returns true if the given object is a map with the same mappings.
11.public V get(Object key)The get() methodology of ConcurrentHashMap class Returns the value to which the specified key is mapped.
12.getOrDefault(Object key, V defaultValue)The getOrDefault() method of ConcurrentHashMap class shows a value to which a specified key is mapped, or the given default value if the map contains no mapping for a key. hashCode()The hashcode() method of ConcurrentHashMap class Returns the hash code value for this Map, i.e., a sum of, for each key-value pair in the map, key.hashCode() ^ value.hashCode().
14.Enumeration<K> keys()The keys() method of ConcurrentHashMap class returns an enumeration of the keys in this table.



keyset(V mappedValue) 

The keyset() method returns all the set of keys present in the map.
16.long mappingCount ( )This method returns the number of mapping present in concurrentHashMap.
17.merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)merge( ) method is used for merging sets if the specified key is not linked with the value.
18.isEmpty( )It returns true if map does not contain any key-value mapping.
19.Keys( )Returns the keys of the table
20.newKeySet( )This method creates a new set key backed by concurrentHashMap.
21.put(K key, V value)This method maps the specific key to its value.
22.void putAll(Map<? extends K,? extends V> m)It copies all the mapping for a particular map to this one.
23.putIfAbsent(K key, V value)This method maps if the particular key is not already associated with a value then associates it with the given value of the map.
24.remove(Object key)

public boolean remove(Object key, Object value)

This method removes the key and its corresponding value from this map. If the key is not on the map, then this method does nothing.
25.replace(K key, V value)

public boolean replace(K key, V oldValue, V newValue)

This replace( ) method replace the entry for a key only if it is mapped to that given value or with some value.
26.String toString( )It returns a string representation of this map. The string is consists of keys and its values on the map.​(long parallelismThreshold, BiFunction<K, V, U> searchFunction)This method returns the result of each search on the map and null if nothing is found.
28.size( )This size( ) method returns the total number of mapping between key-value.
29.values( )This method returns all the values contained in this map in a collection way.
30.forEach(long parallelismThreshold, BiConsumer<K, V> action)This method does the action for each key-value in the map.


See the following code example.

#Example1: computeIfAbsent

See the following code example.

import java.util.concurrent.*;
import java.util.*;
class HashcomputeIfAbsent
  static void main(String args[])
    HashMap<String, Integer> mapcon = new HashMap<>();
    mapcon.put("Shouvik", 80);
    mapcon.put("Sounak", 60);
    mapcon.put("Shubh", 75);
    System.out.println("Values :/n" + mapcon.toString());
    mapcon.computeIfAbsent("deb", k->60 + 75);
    mapcon.computeIfAbsent("ram", k->6 * 5);
    System.out.println("HashMap after computeIfAbsent :/n" + mapcon);

See the output.


ConcurrentHashMap in Java

#Example2: containsValue()

See the following program.

import java.util.*;
import java.util.concurrent.*;
class HashcontainsValue
  static void main(String[] args)
    ConcurrentHashMap<String, Integer> map2 = new ConcurrentHashMap<String, Integer>();
    map2.put("A1", 90);
    map2.put("B1", 80);
    map2.put("C1", 75);
    map2.put("AA", 95);
    System.out.println("Mapping :" + map2);
    System.out.println("is 75 present? :" + map2.containsValue(75));

See the following output.

Java ConcurrentHashMap Tutorial

Finally, ConcurrentHashMap in Java Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.