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
is the general-purposeMap
commonly used when you have no special needs.LinkedHashMap
extendsHashMap
, 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
too maintains an order, but uses either (a) the “natural” order, meaning the value of thecompareTo
method on the key objects defined on theComparable
interface, or (b) invokes aComparator
implementation you provide.TreeMap
implements both theSortedMap
interface, and its successor, theNavigableMap
interface.
- NULLs:
TreeMap
does not allow a NULL as the key, whileHashMap
&LinkedHashMap
do.- All three allow NULL as the value.
HashTable
is legacy, from Java 1. Supplanted by theConcurrentHashMap
class. Quoting the Javadoc:ConcurrentHashMap
obeys the same functional specification asHashtable
, and includes versions of methods corresponding to each method ofHashtable
.