What is the difference between HashMap
, LinkedHashMap
and TreeMap
in Java?
I don\'t see any difference in the output as all the three
@Amit: SortedMap
is an interface whereas TreeMap
is a class which implements the SortedMap
interface. That means if follows the protocol which SortedMap
asks its implementers to do.
A tree unless implemented as search tree, can't give you ordered data because tree can be any kind of tree. So to make TreeMap work like Sorted order, it implements SortedMap ( e.g, Binary Search Tree - BST, balanced BST like AVL and R-B Tree , even Ternary Search Tree - mostly used for iterative searches in ordered way ).
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements SortedMap<K,V>, Cloneable, Serializable
In NUT-SHELL
HashMap
: gives data in O(1) , no ordering
TreeMap
: gives data in O(log N), base 2. with ordered keys
LinkedHashMap
: is Hash table with linked list (think of indexed-SkipList) capability to store data in the way it gets inserted in the tree. Best suited to implement LRU ( least recently used ).
HashMap makes absolutely not guarantees about the iteration order. It can (and will) even change completely when new elements are added. TreeMap will iterate according to the "natural ordering" of the keys according to their compareTo() method (or an externally supplied Comparator). Additionally, it implements the SortedMap interface, which contains methods that depend on this sort order. LinkedHashMap will iterate in the order in which the entries were put into the map
Look at how performance varying..
Tree map which is an implementation of Sorted map. The complexity of the put, get and containsKey operation is O(log n) due to the Natural ordering
Hash map doesn't preserves the insertion order.
Example. Hashmap
If you are inserting keys as
1 3
5 9
4 6
7 15
3 10
It can store it as
4 6
5 9
3 10
1 3
7 15
Linked Hashmap preserves the insertion order.
Example.
If you are inserting keys
1 3
5 9
4 6
7 15
3 10
It will store it as
1 3
5 9
4 6
7 15
3 10
same as we insert.
Tree map stores the vales in Increasing Order Of Keys.
Example.
If you are inserting keys
1 3
5 9
4 6
7 15
3 10
It will store it as
1 3
3 10
4 6
5 9
7 15
See where each class is in the class hierarchy in the following diagram (bigger one). TreeMap implements SortedMap
and NavigableMap
while HashMap
doesn't.
HashTable
is obsolete and the corresponding ConcurrentHashMap
class should be used.
These are different implementations of the same interface. Each implementation has some advantages and some disadvantages (fast insert, slow search) or vice versa.
For details look at the javadoc of TreeMap, HashMap, LinkedHashMap.
All three represent mapping from unique keys to values, and therefore implement the Map interface.
HashMap is a map based on hashing of the keys. It supports O(1) get/put operations. Keys must have consistent implementations of hashCode() and equals() for this to work.
LinkedHashMap is very similar to HashMap, but it adds awareness to the order at which items are added (or accessed), so the iteration order is the same as insertion order (or access order, depending on construction parameters).
TreeMap is a tree based mapping. Its put/get operations take O(log n) time. It requires items to have some comparison mechanism, either with Comparable or Comparator. The iteration order is determined by this mechanism.