1.

What is the time complexity of putting and getting an element from such structure? Which Map implementation provide better performance?

Answer»

The time complexity of HASHMAP to getting the elements is usually from O(N) to O(Log(n)), when there is no collision OCCURS and the worst case is O(n) to O(1), because when collision occurs and HashMap needs to travel till the end of the linked list. Now in the case of putting the elements in HashMap, is always O(1). Below classes is the implementation of Map interface.

  • LinkedHashMap,
  • HashMap
  • Hashtable,
  • WeakHashMap,
  • IdentityHashMap,
  • ConcurrentHashMap,
  • TreeMap, and
  • EnumMap.

However, as PER the MEMORY performance basis, the WeakHashMap gives better performance. But it doesn’t provide the thread safety if one needs to achieve the thread safety, to achieve the thread safety one needs to use the ConcurrentHashMap because it provides thread safety without using synchronization.



Discussion

No Comment Found