ConcurrentMap interface

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

 

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

 

ConcurrentMap interface method:

ConcurrentMap method Description 
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

 

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

 

References:

 

Happy Coding !!
Happy Learning !!

ConcurrentHashMap class
Concurrent Collection