What is the difference between HashMap
, LinkedHashMap
and TreeMap
in Java?
I don\'t see any difference in the output as all the three
All three classes HashMap
, TreeMap
and LinkedHashMap
implements java.util.Map
interface, and represents mapping from unique key to values.
HashMap
A HashMap
contains values based on the key.
It contains only unique elements.
It may have one null key and multiple null values.
It maintains no order.
public class HashMap
LinkedHashMap
LinkedHashMap
contains values based on the key.It is same as HashMap instead maintains insertion order. //See class deceleration below
public class LinkedHashMap
TreeMap
TreeMap
contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.It is same as HashMap
instead maintains ascending order(Sorted using the natural order of its key.).
public class TreeMap
Hashtable
It is a legacy class.
public class Hashtable
Ref: http://javarevisited.blogspot.in/2015/08/difference-between-HashMap-vs-TreeMap-vs-LinkedHashMap-Java.html