The ConcurrentHashMap in Java is a part of the Java Collection Framework which provides a concurrent version of the standard HashMap. The main advantage of ConcurrentHashMap over HashMap is that it allows for simultaneous reads and writes via multiple threads without external synchronization.
Key Points:
Concurrency Level: ConcurrentHashMap allows concurrent threads to read and write without locking the entire map.
Thread-Safe: It is thread-safe without synchronizing the whole map.
Performance: Retrieval operations generally do not block.
Nulls: Neither keys nor values can be null.
Functions: Uses a combination of CAS (Compare-And-Swap) operations and internal locks.
Creating and Adding Key-Value Pairs to Java’s ConcurrentHashMap
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args) {
// Creating a ConcurrentHashMap
ConcurrentHashMap<String, Integer> studentGrades = new ConcurrentHashMap<>();
// Adding key-value pairs
studentGrades.put("Amit", 85);
studentGrades.put("Rohan", 90);
studentGrades.put("Priya", 88);
System.out.println("ConcurrentHashMap content: " + studentGrades);
}
}
Output:
ConcurrentHashMap content: {Amit=85, Rohan=90, Priya=88}
Explanation:
1. ConcurrentHashMap: It's a part of the java.util.concurrent package. Unlike HashMap, it is thread-safe and allows multiple threads to perform retrieval operations. However, the default concurrency level, which defines the number of concurrently updating threads, is set to 16.
2. put: This method is used to add a key-value pair to the ConcurrentHashMap. It works similarly to the put method in a regular HashMap, but is designed to be thread-safe.
Using ConcurrentHashMap, we can store key-value pairs in a thread-safe manner, ensuring safe concurrent access and updates.
Creating a ConcurrentHashMap from Other Maps
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapFromOtherMaps {
public static void main(String[] args) {
// Create a HashMap and populate it with some data
Map<String, Integer> hashMap = new HashMap<>();
hashMap.put("One", 1);
hashMap.put("Two", 2);
hashMap.put("Three", 3);
// Create a ConcurrentHashMap from the HashMap
ConcurrentHashMap<String, Integer> concurrentMap = new ConcurrentHashMap<>(hashMap);
// Print the contents of the ConcurrentHashMap
concurrentMap.forEach((key, value) -> System.out.println(key + " -> " + value));
}
}
Output:
One -> 1 Two -> 2 Three -> 3
Explanation:
1. We first create a HashMap named hashMap and populate it with some key-value pairs.
2. We then use the constructor of ConcurrentHashMap that accepts a Map as its argument to create a ConcurrentHashMap named concurrentMap.
3. The forEach(BiConsumer<? super K, ? super V> action) method is used to iterate over the concurrentMap and print its contents.
In essence, this example demonstrates how to easily convert a regular HashMap or any other Map implementation into a ConcurrentHashMap.
Accessing Keys and Values in Java’s ConcurrentHashMap
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapAccess {
public static void main(String[] args) {
// Creating a ConcurrentHashMap and adding key-value pairs
ConcurrentHashMap<String, Integer> studentGrades = new ConcurrentHashMap<>();
studentGrades.put("Amit", 85);
studentGrades.put("Rohan", 90);
studentGrades.put("Priya", 88);
// Accessing keys
for (String student : studentGrades.keySet()) {
System.out.println("Student name: " + student);
}
// Accessing values
for (Integer grade : studentGrades.values()) {
System.out.println("Grade: " + grade);
}
}
}
Output:
Student name: Amit Student name: Rohan Student name: Priya Grade: 85 Grade: 90 Grade: 88
Explanation:
1. keySet(): This method retrieves a Set of all the keys contained in the ConcurrentHashMap. It's worth noting that the set is a view on the keys, meaning changes in the map are reflected in the set and vice-versa.
2. values(): This method returns a Collection of all the values stored in the ConcurrentHashMap. Similar to keySet(), changes in the map are mirrored in the returned collection.
The iteration over the keySet() and values() allows us to access individual keys and values in the ConcurrentHashMap. As the map is thread-safe, these operations can be safely executed even in concurrent environments.
Removing Keys from Java’s ConcurrentHashMap
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapRemove {
public static void main(String[] args) {
// Creating a ConcurrentHashMap and adding key-value pairs
ConcurrentHashMap<String, Integer> studentGrades = new ConcurrentHashMap<>();
studentGrades.put("Amit", 85);
studentGrades.put("Rohan", 90);
studentGrades.put("Priya", 88);
System.out.println("Before Removal: " + studentGrades);
// Removing a key-value pair using the key
studentGrades.remove("Amit");
System.out.println("After Removal of Amit: " + studentGrades);
}
}
Output:
Before Removal: {Amit=85, Rohan=90, Priya=88} After Removal of Amit: {Rohan=90, Priya=88}
Explanation:
1. remove(Object key): This method is utilized to remove the key-value pair associated with the specified key from the ConcurrentHashMap. If the key doesn't exist in the map, the method has no effect.
By using the remove method, we can easily and safely remove key-value pairs from a ConcurrentHashMap even in concurrent environments.
Obtaining entrySet, keySet, and values from Java’s ConcurrentHashMap
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map.Entry;
public class ConcurrentHashMapSetsValues {
public static void main(String[] args) {
// Creating a ConcurrentHashMap and adding key-value pairs
ConcurrentHashMap<String, Integer> studentGrades = new ConcurrentHashMap<>();
studentGrades.put("Amit", 85);
studentGrades.put("Rohan", 90);
studentGrades.put("Priya", 88);
// Obtaining and displaying entrySet
for (Entry<String, Integer> entry : studentGrades.entrySet()) {
System.out.println("Entry: " + entry.getKey() + " - " + entry.getValue());
}
// Obtaining and displaying keySet
for (String key : studentGrades.keySet()) {
System.out.println("Key: " + key);
}
// Obtaining and displaying values
for (Integer value : studentGrades.values()) {
System.out.println("Value: " + value);
}
}
}
Output:
Entry: Amit - 85 Entry: Rohan - 90 Entry: Priya - 88 Key: Amit Key: Rohan Key: Priya Value: 85 Value: 90 Value: 88
Explanation:
1. entrySet(): This method retrieves a Set view of the mappings contained in the ConcurrentHashMap. Each element in the set is a map entry (Entry<K,V>).
2. keySet(): This method provides a Set view of the keys present in the ConcurrentHashMap.
3. values(): It offers a Collection view of the values stored in the ConcurrentHashMap.
All the views (entrySet, keySet, and values) reflect the changes made in the original map, ensuring consistency between them and the actual ConcurrentHashMap.
Iterating Over Java’s ConcurrentHashMap in Different Ways
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map.Entry;
import java.util.Iterator;
public class ConcurrentHashMapIteration {
public static void main(String[] args) {
// Creating a ConcurrentHashMap and adding key-value pairs
ConcurrentHashMap<String, Integer> studentGrades = new ConcurrentHashMap<>();
studentGrades.put("Amit", 85);
studentGrades.put("Rohan", 90);
studentGrades.put("Priya", 88);
// 1. Using forEach and lambda expression
System.out.println("Using forEach and lambda:");
studentGrades.forEach((key, value) -> System.out.println(key + " -> " + value));
// 2. Iterating using entrySet and for-each loop
System.out.println("\nUsing entrySet:");
for (Entry<String, Integer> entry : studentGrades.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
// 3. Iterating using keySet and for-each loop
System.out.println("\nUsing keySet:");
for (String key : studentGrades.keySet()) {
System.out.println(key + " -> " + studentGrades.get(key));
}
// 4. Iterating using iterators
System.out.println("\nUsing iterators:");
Iterator<Entry<String, Integer>> iterator = studentGrades.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Integer> entry = iterator.next();
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}
Output:
Using forEach and lambda: Amit -> 85 Rohan -> 90 Priya -> 88 Using entrySet: Amit -> 85 Rohan -> 90 Priya -> 88 Using keySet: Amit -> 85 Rohan -> 90 Priya -> 88 Using iterators: Amit -> 85 Rohan -> 90 Priya -> 88
Explanation:
1. forEach(BiConsumer<? super K, ? super V> action): Executes the given action for each key-value pair in the map.
2. entrySet(): Provides a Set view of the mappings contained in the map. Can be used with a for-each loop to iterate over the entries.
3. keySet(): Supplies a Set view of the keys. Used with a for-each loop to iterate over keys and then fetch the corresponding values.
4. iterator(): Returns an iterator over the entry set of the map, which can be used to traverse the map's entries.
These are common ways to iterate over a ConcurrentHashMap, providing flexibility depending on the use-case.
Using ConcurrentHashMap with User-Defined Objects: Student
import java.util.concurrent.ConcurrentHashMap;
import java.util.Objects;
class Student {
private String name;
private int rollNo;
// Constructor
public Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
// Getters and setters omitted for brevity
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return rollNo == student.rollNo &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, rollNo);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", rollNo=" + rollNo +
'}';
}
}
public class ConcurrentHashMapWithCustomObject {
public static void main(String[] args) {
// Create a ConcurrentHashMap with Student as the key and String as the value (e.g., grade of the student)
ConcurrentHashMap<Student, String> studentGrades = new ConcurrentHashMap<>();
Student amit = new Student("Amit", 101);
Student rohan = new Student("Rohan", 102);
// Add students to the map
studentGrades.put(amit, "A");
studentGrades.put(rohan, "B");
// Print the students and their grades
studentGrades.forEach((student, grade) -> System.out.println(student + " -> " + grade));
}
}
Output:
Student{name='Amit', rollNo=101} -> A Student{name='Rohan', rollNo=102} -> B
Explanation:
1. Student class: Represents a student with name and rollNo. For the Student class to work correctly as a key in ConcurrentHashMap, it needs to override equals() and hashCode() methods. These methods ensure that different instances of Student with the same properties are treated as equal when looking them up in the map.
2. studentGrades: This is a ConcurrentHashMap where the key is a Student object, and the value is a String representing the student's grade. We add two students, Amit and Rohan, and their respective grades to this map.
3. forEach(BiConsumer<? super K, ? super V> action): This method is used to iterate over the studentGrades map and print each student along with their grade.
The example demonstrates the use of a user-defined object (Student) as the key in a ConcurrentHashMap and how to effectively store and retrieve values using custom object keys.