What main Implementations Map interface

What main Implementations Map interface

Answer: Map implementations grouped into three category:

1. General Purpose Map Implementations: There are three general purpose implementations those are:

  • HashMap: HashMap is very suitable if you want maximum speed and not care about iteration order
  • TreeMap: TreeMap is very suitable if you need SortedMap operations or key ordered Collection view iteration
  • LinkedHashMap. LinkedHashMap is very suitable if you want proximate HashMap performance and insertion order iteration

The situation for Map is analogous to Set. For example: everything else in Set Implementations section also applies to the Map implementations.

2. Special-Purpose Map Implementations

There are three special purpose implementations those are:

  • EnumMap: EnumMap, This is internally implemented as array and it’s a high-performance Map implementation to use with enum keys. EnumMap implementation combines richness including safety of Map interface with speed approaching like array. If you want to create map and enum to value then it is always preferable to use EnumMap in preference to an array.
  • WeakHashMap: It is implementation of Map interface which stores only weak references of its keys. By storing only weak references allows key value pair to be garbage collected whenever key has no longer referenced outside of WeakHashMap. This class also provides easiest way to harness the actual power of the weak references. It is useful for implementing “registry like” data structures where utility of entry disappears when it’s key has no longer reachable by any of thread.
  • IdentityHashMap: This is an identity based Map implementation based on hash table. It is useful for topology preserving kind object graph transformations example as serialization or deep copying. When perform such kind of transformations one need to maintain identity based “node table” which keeps track of the objects have already been seen.

3. Concurrent Map Implementations

ConcurrentMap interface contains inside the java.util.concurrent package which extends Map with atomic putIfAbsent, remove, and replace methods and also ConcurrentHashMap implementation of interface.
ConcurrentHashMap is high performance implementation backed up by hash table and its highly concurrent. Implementation never blocks whenever performing retrievals and allows client to select concurrency level for updates. It is intended as drop in replacement to Hashtable. In addition to implementing ConcurrentMap it supports all the legacy methods peculiar to Hashtable.

Don’t miss All java collections interview questions

Leave a Reply

Your email address will not be published. Required fields are marked *