← Back to list

Concurrent Collections in Java PART ONE.

Hi All,

Heshan Umayanga · 2026-06-01 06:22 · 0 claps · 3.5 min read
#java-collection #java-concurrency #java-thread-safety #concurrentmodification
Open on Medium ↗
Wiki topics: SAF · Safety & Alignment

Concurrent Collections in Java PART ONE.

Hi All,

from pexel.com

from pexel.com

In this article, I wish to share my own experience with famous interview questions and concepts in the Java language called Concurrent Collection. This package is in java.util.concurrent package.

I had several experiences with collections and concurrent collections during interviews. Among them, concurrent collections-related questions are more attractive, so I wish to share my experiences with you.

I assume you already know the collections framework in Java; it is a common and vital question in interviews.

First, we find out the necessity of Concurrent Collections. We already have a collection framework. Why do we actually want them in our code?.

Collections framework already has a thread-safe and synchronised implementation of the collection interface, like Vector, Hashtable, etc. Not all collections are thread-safe.

Thread-safe means it only allows access to the shared resource one thread at a time, by avoiding deadlocks. Normally, we address this issue using synchronized key word. But in collections, Java already handle deadlock situations using concurrent collections.

Concurrent collection is also an interface-like collection; it is a typical solution for a collection framework to handle concurrency. And concurrent collections are extended with serializable, callable, and runnable interfaces.

There are major implementations of concurrent collection, such as

  • ConcurrentHashMap
  • CopyOnWriteArrayList
  • CopyOnWriteArraySet

pic from pexel.com

pic from pexel.com

Let’s get started with Concurrent Map.

Concurrent HashMap is an implementation of ConcurrentMap and the Map interface, but a map is not a collection interface.

And you already know a map is a pair data structure like a key-value pair structure, so it has features from both the Map interface and concurrentMap interface via inheritance.

ConcurrentHashMap has methods called

  • putIfAbsent(Object key,Object value);
  • boolean remove(Object key,Object value);
  • boolean replace(Object key,Object oldvalue,Object newValue);

If you remember the collections framework,

You can easily look and get an idea about how ConcurrentHashMap is synchronised.

In the putIfAbsent() method, it is used.

Object putIfAbsent(Object Key,Object value){
  if(!map.containKey(key)){
        map.put(key,value);
  }else{
        return map.get(key);
  }
}

In a normal HashMap, it puts the value directly and changes the value related to the key, without considering the existing value.

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;

class Main {

public static void main(String[] args) {
// Creating ConcurrentMap using ConcurrentHashMap
Map<String, Integer> numbers = new HashMap<>();

// Insert elements to map
numbers.put("Two", 2);
numbers.put("One", 1);
numbers.put("Three", 3);
System.out.println("Map: " + numbers);

//it will display like a dictionary
what if I add value for key called One,
numbers.put("One", 10);

//it shows update value for key
System.out.println("Map: " + numbers);
[{"One":10},{"Two":2},{"Three":10}]

In a normal HashMap, it doesn't care about existing values in the Map. So this is enough and works fine in local and simple scenarios, but in the real world, this will raise ConcurrentModificationException.

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ConcurrentMapDemo {
    public static void main(String[] args) {
        // Instantiate a concurrent map
        ConcurrentMap<String, Integer> numberConcurrentMap= new ConcurrentHashMap<>();

        numberConcurrentMap.put("One", 1);
        numberConcurrentMap.put("Two", 2);
        numberConcurrentMap.put("Three", 3);

        // Atomic operation: Add only if missing
        numberConcurrentMap.putIfAbsent("One", 101);
        numberConcurrentMap.putIfAbsent("Two", 102);
        numberConcurrentMap.putIfAbsent("Three", 103);
        System.out.println("Final Inventory: " +numberConcurrentMap ); 
        // Output: { One0=1, Two=2, Three=3}
    }
}

But we can change the values of the key using the replace method like this,

boolean success1 =numberConcurrentMap.replace("One", 1, 101);
System.out.println("Final Inventory: " +numberConcurrentMap ); 
        // Output: { One0=101, Two=2, Three=3}

//syntax for replace method is
// boolean success1 =numberConcurrentMap.replace(Object key, Object existingvalue, Object newValue); 
// We can remove entry because 101 key
// is associated with For value
numberConcurrentMap.remove(101, "Geeks");
numberConcurrentMap.remove(101); //also possible

What if more than one thread tries to change the value from one key? It will raise an exception, and this issue will be solved using ConcurrentHashMap’s concurrency level. Usually, it has 16 concurrency levels by default.

And each segment has its own lock instead of one lock for the whole structure, like a HashMap.Will explain this by contrasting HashMap, hashTable and ConcurrentHashMap.

I think this content is enough for PART One of Concurrent Collections in Java Series.

Happy coding…

References-

pic from pexel.com

pic from pexel.com


메타데이터
post_id
1315a07c1743
slug
concurrent-collections-in-java-part-one-1315a07c1743
url
https://medium.com/@heshanu97/concurrent-collections-in-java-part-one-1315a07c1743
canonical_url
https://medium.com/@heshanu97/concurrent-collections-in-java-part-one-1315a07c1743
author_url
https://medium.com/@heshanu97
status
ok
fetched_at
2026-07-09 00:50:33