In this blog entry, I'll focus on how
Hashtableis different from
HashMap, but most of these differences apply to the contrasting of
Hashtablehas been around longer than
Hashtablewas introduced with JDK 1.0 and so predates the birth of the Java Collections Framework and the naming convention of Collections in that framework. The other two types of collections that pre-dated the Java Collections Framework were
The Java Collections Framework was introduced with JDK 1.2 (among the most exciting of the Java releases) and introduced many collections including my favorites
ArrayList. Also with JDK 1.2, the
Vectorclasses were retrofitted to be part of this new Java Collections Framework and, as part of this, to implement interfaces from that framework.
Hashtablewas altered to implement Map and
Vectorwas similarly altered to implement List.
Hashtablewas retrofitted to implement
Mapand become part of the Java Collections Framework, its name obviously could not be changed because of backwards compatibility issues. Therefore, its "table" portion of the name could not have the "t" changed to uppercase "T." In fact, there is a naming convention for implementations and interfaces in the Java Collection Framework and neither
Vectorcould be changed to meet this convention. Most implementations in the Java Collections Framework (such as
TreeSet) have their name formed from an implementation detail as the first portion of the name and having the name end with the interface implemented.
The main Java Collections Framework documentation contains an Overview section with a "Collections Implementations" subsection that talks about this naming convention and shows a table with interfaces on the rows and implementations along the columns. Not all collections are shown in this table. For example, Map collections not shown in the table include
So, other than name, what differences now exist between a
HashMapif they both are Maps? This question is most quickly answered in the Javadoc documentation for
HashMap, which says (in a parenthetical statement), "(The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.)" The
HashMap's allowance of nulls is one distinguishing difference from
Hashtable. The other distinguishing difference (not being synchronized) is also the distinguishing difference between
HashMapcan be used in multiple thread situations if the synchronization wrappers are used. This is easily accomplished through a call to the Collections class like this:
In this blog entry, I tried to have each link to Java Collection Framework reference a different resource on this widely used piece of Java. The references below are to other articles and blog entries covering the differences between
jGuru: What are the differences between HashMap and Hashtable?
Difference Between HashMap and HashTable
Hashtable, HashMap, & Properties
Java Hashmap or Hashtable
Hashtable or HashMap?
HashMap Versus Hashtable