Java 5 – ConcurrentMap interface

In this article, we will discuss ConcurrentMap interface with all its important methods in detail

1. ConcurrentMap interface:

  • To represent a group of key-value pairs as a single unit/entity, which is a thread-safe version of Map/HashMap
  • That’s it allows simultaneous/concurrent access to read/modify Map object
  • Map allows only unique keys to be inserted but it can have duplicate values against these unique keys
  • Implementation class for ConcurrentMap interface is ConcurrentHashMap
  • ConcurrentMap interface defines additional atomic methods, in addition to methods inherited from Map interface
  • Present in java.util.concurrent package
  • Extends java.util.Map interface
2-concurrentmap-interace-in-java

Source: Team BenchResources.Net

2. ConcurrentMap interface method:

ConcurrentMap methodDescription 
Object putIfAbsent(Object key, Object value);to add a new entry to ConcurrentMap, if the specified key isn’t present in the invoking Map
boolean remove(Object key, Object value);Removes an entry from invoking Map for the specified key-value pair combination
boolean replace(Object key, Object oldValue, Object newValue);replaces old value with new value for the specified key

 

Note: old key-value pair combination should be present

boolean replace(Object key, Object value);replaces the entry for a key only if currently mapped to some value

Note :

  • ConcurrentHashMap is implementation class of ConcurrentMap
  • all properties will be inherited from ConcurrentMap to ConcurrentHashMap

3. Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 5 - ConcurrentHashMap class with example
Java 5 - Introduction to Concurrent Collection