1.

Describe various implementations of the Map interface and their use case differences and map hierarchy diagram?

Answer»

1. HASHMAP:

  • HashMap is an implementation of map interface, where it's utilizing the hashing algorithms to calculate the index position using hashcode() VALUES from the keys.
  • HashMap guaranteed the time complexity of O(1) for insertion and searching.
  • Code Snippet: Map map = new HashMap();  
  • Use case: Sometimes when the DATA is huge and at the same time, we need faster insertion and retrieval because the time complexity is O(1) and regardless considering the order of the data, I would say that in this case, one can use the HashMap.

2. LinkedHashMap

Linked HashMap is an implementation of the map interface along with the hashing algorithms it also supports the ordering according to the insertion order.

Code Snippet: Map map = new LinkedHashMap();

Use Case: LinkedHashMap is useful whenever we need to maintain the ordering of keys to match the ordering of insertion. along with that when we need to handle the caching mechanism.

3. TreeMap

TreeMap is an implementation of the map interface, where it follows the Tree data structure which is RedBlack Tree implementation and follows the natural order  

Code Snippet: Map map = new TreeMap();

Use case: One can use the TreeMap when we need the natural ordering, or we need to sort the record alphabetically or ascending and descending order.



Discussion

No Comment Found