In this Collection framework tutorial we will learn what are differences and similarities between java.util.HashMap, java.util.Hashtable, java.util.LinkedHashMap and java.util.TreeMap in java.
Differences between java.util.HashMap vs java.util.Hashtable vs java.util.LinkedHashMap vs java.util.TreeMap >
Property
|
HashMap
|
Hashtable
|
LinkedHashMap
|
TreeMap
| |
1
|
Insertion order
|
HashMap does not maintains insertion order in java.
|
Hashtable does not maintains insertion order in java.
|
LinkedHashMap maintains insertion order in java.
|
TreeMap is sorted by natural order of keys in java.
|
2
|
Performance
|
HashMap is not synchronized, hence its operations are faster as compared to Hashtable.
|
Hashtable is synchronized, hence its operations are slower as compared HashMap.
If we are working not working in multithreading environment jdk recommends us to use HashMap.
|
LinkedHashMap must be used only when we want to maintain insertion order. Time and space overhead is there because for maintaining order it internally uses Doubly Linked list.
|
TreeMap must be used only when we want sorting based on natural order. Otherwise sorting operations cost performance. (Comparator is called for sorting purpose)
|
3
|
Null keys and values
|
HashMap allows to store one null key and many null values i.e. many keys can have null value in java.
|
Hashtable does not allow to store null key or null value.
Any attempt to store null key or value throws runtimeException (NullPointerException) in java.
|
LinkedHashMap allows to store one null key and many null values i.e. any key can have null value in java.
|
TreeMap does not allow to store null key but allow many null values.
Any attempt to store null key throws runtimeException (NullPointerException) in java.
|
4
|
Implements which interface
|
Hashtable implements java.util.Map
|
LinkedHashMap implements java.util.Map
|
TreeMap implements
java.util.Map
java.util.SortedMap
java.util.NavigableMap
| |
5
|
Implementation uses?
|
HashMap use buckets
|
Hashtable use buckets
|
LinkedHashMap uses doubly linked lists
|
TreeMap uses Red black tree
|
6
|
Complexity of put, get and remove methods
|
O(1)
|
O(1)
|
O(1)
overhead of updating Doubly Linked list for maintaining order it internally uses.
|
O(log(n))
|
7
|
Extends java.util.Dictionary (Abstract class, which is obsolete)
|
HashMap doesn’t extends Dictionary.
|
Hashtable extends Dictionary (which maps non-null keys to values. In a given Dictionary we can look up value corresponding to key)
|
LinkedHashMap doesn’t extends Dictionary.
|
TreeMap doesn’t extends Dictionary.
|
8
|
Introduced in which java version?
|
HashMap was introduced in second version of java i.e. JDK 2.0
|
Hashtable was introduced in first version of java i.e. JDK 1.0
But it was refactored in java 2 i.e. JDK 1.2 to implement the Map interface, hence making it a member of member of the Java Collections Framework.
|
LinkedHashMap was introduced in fourth version of java i.e. JDK 4.0
|
TreeMap was introduced in second version of java i.e. JDK 2.0
|
So far we have learned what are differences between java.util.HashMap, java.util.Hashtable, java.util.LinkedHashMap and java.util.TreeMap in java.
Now we will learn similarities between java.util.HashMap, java.util.Hashtable, java.util.LinkedHashMap and java.util.TreeMap in Collection framework in java.
Similarity between HashMap vs Hashtable vs LinkedHashMap vs TreeMap >
Property
|
java.util.HashMap,
java.util.Hashtable,
java.util.LinkedHashMap and java.util.TreeMap.
| |
1
|
iterators are fail-fast
|
The iterators returned by the iterator() method of HashMap,Hashtable, LinkedHashMap and TreeMap all are fail-fast >
all three iterators are fail-fast
|
So in this Collection framework tutorial we learned what are differences and similarities between java.util.HashMap, java.util.Hashtable, java.util.LinkedHashMap and java.util.TreeMap in java.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
ConcurrentHashMap in java - with Segments formation in detail with diagram in java
HashMap and Hashtable - Similarity and Differences in java
Important Similarity and Differences in java >
HashMap vs IdentityHashMap - Similarity and Differences with program in java
TreeMap vs ConcurrentSkipListMap - Similarity and Differences with program in java
Top Collection Interviews question and answers in java >
COLLECTION - Top 50 interview questions and answers in java for fresher and experienced
COLLECTION - Top 50 interview questions and answers in java for fresher and experienced