Updated June 19, 2023
Definition of ConcurrentHashMap in Java
ConcurrentHashMap in Java is introduced in Java 1.5, and this package belongs to the package of java util concurrent. This package of concurrent HashMap implements the serializable and ConcurrentMap interface. The concurrent HashMap in Java is a HashMap enhancement. When working with threads, HashMap is not a good choice because the performance of HashMap is not good compared to the concurrent HashMap in Java.
Key Takeaways
- Inserting the null objects is impossible by using concurrent HashMap as per the value of the key in Java. In Java hash table provides the concurrency for the update.
- The class of concurrent HashMap contains the same specification as the hash table and will include the version methods.
What is ConcurrentHashMap in Java?
The data structure of concurrent HashMap in java is hashable. The class of concurrent HashMap is thread-safe, so multiple threads operates a single object without complications of another object. When a number of threads are applicable for a read operation without locking the object of concurrent HashMap, we can divide the object into a number of segments per the concurrency level.
The default level of concurrency in concurrent HashMap is 16. Into the concurrent HashMap, the thread performs the retrieval operation, but the object that was updated, then the thread locks the particular segment on which the thread is operating. This type of concurrent HashMap locking mechanism is known as a bucket or segment locking. So, at that time, 16 update operations are performed from specified threads.
How to Create ConcurrentHashMap in Java?
The HashMap concurrent class in java is nothing but the HashMap framework that provides the thread-safe map. The multiple threads access the map once without affecting any consistency.
Example #1
The below example shows how we can create the concurrent HashMap in java as follows.
Code:
import java.util.concurrent.*;
class concurrent {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, String> st
= new ConcurrentHashMap<>();
st.put (20, "ABC");
st.put (31, "PQR");
st.put (10, "PQR");
st.putIfAbsent (31, "ABC");
st.remove (31, "PQR");
st.putIfAbsent (20, "ABC");
st.replace (31, "ABC", "MNP");
System.out.println (st);
}
}
Output:
Example #2
In the below example, we are inserting the mapping into the concurrent HashMap we are using, put and put all methods as follows.
Code:
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class concurrent {
public static void main(String[] args) {
ConcurrentHashMap<String, String> st
= new ConcurrentHashMap<String, String>();
st.put ("31", "1");
st.put ("42", "1");
st.put ("73", "1");
st.put ("64", "1");
st.put ("55", "1");
st.put ("63", "1");
System.out.println ("Mappings : " + st);
ConcurrentHashMap<String, String> st1 = new ConcurrentHashMap<>();
st1.putAll (st);
System.out.println ("New mappings : " + st1);
}
}
Output:
ConcurrentHashMap in Java Data Structure
Concurrent HashMap implements the data structure of the map that provides the safety of the thread. It will work while dividing the complete hash table array into the portion of segments that allow parallel access to the specified element. The data structure in concurrent HashMap needs high concurrency in our application. The locking of granularity is contained in the bucket level of HashMap.
The data structure in a concurrent HashMap is thread-safe; it does not synchronize the map. Using a data structure in concurrent HashMap, reads are happening fast, and the write is down into the bucket or segment level. The data structure in concurrent HashMap will not contain locking at the object level.
The below example shows a data structure in a concurrent HashMap as follows.
Code:
import java.util.concurrent.*;
class concurrent {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, String> st
= new ConcurrentHashMap<Integer, String>();
st.put (91, "ABC");
st.put (75, "PQR");
st.put (67, "ABC");
st.put (13, "cont");
System.out.println ("Mappings : ");
System.out.println (st);
System.out.println ("Value"
+ " 91 is : " + st.get (91));
System.out.println ("Value"
+ " 13 is : " + st.get(13));
}
}
Output:
List of Class Methods
Java concurrent HashMap contains multiple methods. Below are the concurrent HashMap method as follows.
- Clear() – This method removes all the mapping of the specified map.
- Compute() – This method attempts to compute a specified value and key.
- Computer absent() – This method maps the new value if a specified key value is present.
- Contains() – Test the key maps in the specified key value of the table.
- containsKey() – Test the object of its key value.
- containsValue – Returns true when this method contains one or more keys and their value.
- Elements() – It will return the enumeration values.
- entrySet() – It will return the mappings view that contains the map.
- Equals() – It will be compared to the specified object.
- forEach() – Acting on key and value.
- forEachEntry() – Performs specified action for every entry.
- forEachKey() – It will perform every action on a non-null transformation.
- forEachValue() – Perform specified action on every value.
- get() – Return value which specified key is mapped with another key.
- getOrDefault() – It will return the value of each key.
- hashCode – It will return the hash code value for every map.
- Keys() – Returning key from the table.
- keySet – Returning the keyset view from the contained map.
- mappingCount – Returning the number of the mapping.
- merge() – It will associate with the given value.
- newKeySet – This method creates the new key set.
- put() – It will map the specified value with the specified key.
- putAll() – It will map all the values with the specified map.
- reduce() – It will return the result, accumulating to the pair of transformations.
- remove() – It will remove the key from the specified map.
- replace() – This method replaces the entry from the specified map value.
- Search() – It will return the non-null result.
- toString – It will return the string representation.
- Values() – This method returns the collection of values.
- Clone() – It will return the copy of the instance.
- isEmpty() – It will return true if the map contains the mapping of key-value pairs.
- Size() – Return the number of key-value mappings.
Examples of ConcurrentHashMap in Java
The below example shows concurrent HashMap in java as follows:
Example #1
In the below example, we are using the put method as follows.
Code:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class concurrent {
public static void main(String[] args) {
Map<String,String> st = new ConcurrentHashMap<String,String>();
st.put ("21", "1");
st.put ("27", "1");
st.put ("35", "1");
System.out.println("Iterator: "+st);
Iterator<String> it = st.keySet().iterator();
while(it.hasNext()){
String key = it.next();
if(key.equals("3")) st.put(key+"new", "new3");
}
System.out.println ("Iterator 1: "+st);
st = new HashMap<String,String>();
st.put("21", "1");
st.put("27", "1");
st.put("35", "1");
System.out.println("Iterator 2: "+st);
Iterator<String> it1 = st.keySet().iterator();
while(it1.hasNext()){
String key = it1.next ();
if (key.equals ("3")) st.put (key+"new", "new3");
}
System.out.println ("After iterator: "+st);
}
}
Output:
Example #2
In the below example, we are using the put and putIfAbsent method as follows.
Code:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class concurrent {
public static void main(String[] args) {
ConcurrentHashMap<Integer, String> pt
= new ConcurrentHashMap<>();
pt.put (20, "AB");
pt.put (31, "PQ");
pt.put (10, "PQ");
pt.putIfAbsent (31, "AB");
pt.remove (31, "PQ");
pt.putIfAbsent (20, "AB");
pt.replace (31, "AB", "MN");
System.out.println (pt);
}
}
Output:
Conclusion
The data structure of concurrent HashMap in java is hashable. The class of concurrent HashMap is thread-safe, so multiple threads operates a single object without complications of another object. ConcurrentHashMap in java is introduced in java 1.5, and this package belongs to the package of java util concurrent. This package of concurrent HashMap implements the serializable and ConcurrentMap interface.
Recommended Articles
We hope that this EDUCBA information on “ConcurrentHashMap in Java” was beneficial to you. You can view EDUCBA’s recommended articles for more information.