Friday, July 26, 2019

HashMap vs LinkedHashMap

HashMap:

public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable

All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>

Direct Known Subclasses:
LinkedHashMap, PrinterStateReasons

LinkedHashMap:

public class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>

All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>

Similarities between HsahMap and LinkedHashMap

  • Iterator returned by HashMap and LinkedHashMap are fail-fast.
  • HashMap and LinkedHashMap both are similar in terms of performance.
  • HashMap and LinkedHashMap both are not synchronized. that means not thread safe.

Difference between HsahMap and LinkedHashMap

HsahMap LinkedHashMap
HsahMap LinkedHashMap
HsahMap was introduced in JDK 2.0. LinkedHashMap was introduced in JDK 4.0.
HsahMap uses the HashTable to store entries. LinkedHashMap uses the HashTable and doubly Linked List to store entries.
HashMap does not maintain the insertion order. LinkedHashMap maintains the insertion order of elements.
HsahMap has a relatively low overhead. LinkedHashMap has a relatively higher overhead.Because it maintain the order of entries
HsahMap extends the AbstractMap and implements Map interface. LinkedHashMap extends the Hashmap and implements Map interface that means it is sub-class of HashMap.
HsahMap requires less memory space compared to LinkedHashMap. LinkedHashMap requires more memory space compared to HashMap because of it maintain the order of entries.Internally LinkedHashMap uses doubly Linked List to maintain order of elements.
  Re-entering(updating value of existing key) a value does not change the insertion order of the LinkedHashMap.
HashMap has constant-time performance O(1) for most operations like put(), get(), remove() and contains().  LinkedHashMap has complexity of O(1) for put(), get(), remove() and contains().


Related Tutorials 

No comments:

Post a Comment