LinkedHashMap and HashMap in Java


LinkedHashMap and HashMap in Java

HashMap and LinkedHashMap are two of the most common used Map implementation in Java. Main difference between HashMap and LinkedHashMap is that LinkedHashMap maintains insertion order of keys, order in which keys are inserted in to LinkedHashMap.
On the other hand HashMap doesn't maintain any order or keys or values. In terms of Performance there is not much difference between HashMap and LinkedHashMap but yes LinkedHashMap has more memory foot print than HashMap to maintain doubly LinkedList which it uses to keep track of insertion order of keys.
 

Difference between LinkedHashMap and HashMap in Java

Now let's see some differences between LinkedHashMap and HashMap in Java: 


1) First and foremost difference between LinkedHashMap and HashMap is order, HashMap doesn't maintain any order while LinkedHashMap maintains insertion order of elements in Java.


2) LinkedHashMap also requires more memory than HashMap because of this ordering feature. As I said before LinkedHashMap uses doubly LinkedList to keep order of elements.


3) LinkedHashMap actually extends HashMap and implements Map interface. 



 

Related Articles

post a comment