What is the difference between HashMap
, LinkedHashMap
and TreeMap
in Java?
I don\'t see any difference in the output as all the three
HashMap
can contain one null key.
HashMap maintains no order.
TreeMap
TreeMap can not contain any null key.
TreeMap maintains ascending order.
LinkedHashMap
LinkedHashMap can be used to maintain insertion order, on which keys are inserted into Map or it can also be used to maintain an access order, on which keys are accessed.
Examples::
1) HashMap map = new HashMap();
map.put(null, "Kamran");
map.put(2, "Ali");
map.put(5, "From");
map.put(4, "Dir");`enter code here`
map.put(3, "Lower");
for (Map.Entry m : map.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
2) TreeMap map = new TreeMap();
map.put(1, "Kamran");
map.put(2, "Ali");
map.put(5, "From");
map.put(4, "Dir");
map.put(3, "Lower");
for (Map.Entry m : map.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
3) LinkedHashMap map = new LinkedHashMap();
map.put(1, "Kamran");
map.put(2, "Ali");
map.put(5, "From");
map.put(4, "Dir");
map.put(3, "Lower");
for (Map.Entry m : map.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
All offer a key->value map and a way to iterate through the keys. The most important distinction between these classes are the time guarantees and the ordering of the keys.
Imagine you passed an empty TreeMap, HashMap, and LinkedHashMap into the following function:
void insertAndPrint(AbstractMap<Integer, String> map) {
int[] array= {1, -1, 0};
for (int x : array) {
map.put(x, Integer.toString(x));
}
for (int k: map.keySet()) {
System.out.print(k + ", ");
}
}
The output for each will look like the results below.
For HashMap, the output was, in my own tests, { 0, 1, -1}, but it could be any ordering. There is no guarantee on the
ordering.
Treemap,the output was,{ -1, 0, 1}
LinkedList,the output was,{ 1, -1, 0}
While there are plenty of excellent Answers here, I'd like to present my own table describing the various Map
implementations bundled with Java 11.
We can see these differences listed on the table graphic:
HashMap
, adding this behavior: Maintains an order, the order in which the entries were originally added. Altering the value for key-value entry does not alter its place in the order.TreeMap
implements both the SortedMap interface, and its successor, the NavigableMap interface.TreeMap
does not allow a NULL as the key, while HashMap
& LinkedHashMap
do.
ConcurrentHashMap
obeys the same functional specification as Hashtable
, and includes versions of methods corresponding to each method of Hashtable
.Just some more input from my own experience with maps, on when I would use each one:
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<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
LinkedHashMap
LinkedHashMap
contains values based on the key.It is same as HashMap instead maintains insertion order. //See class deceleration below
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
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<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, Serializable
Hashtable
It is a legacy class.
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
Ref: http://javarevisited.blogspot.in/2015/08/difference-between-HashMap-vs-TreeMap-vs-LinkedHashMap-Java.html