Friday, March 28, 2008

Hashtable Versus HashMap

I find myself using ArrayList and HashMap as my two favorite "default" Java collections and I use the other collections significantly less frequently than these two favorites. Although I do not use Hashtable or Vector very often, they have their place and their many similarities with my two preferred Collections make it more interesting to focus on how they are different.

In this blog entry, I'll focus on how Hashtable is different from HashMap, but most of these differences apply to the contrasting of Vector and ArrayList as well.

Hashtable has been around longer than HashMap. Hashtable was 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 Vector and array.

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 HashMap and ArrayList. Also with JDK 1.2, the Hashtable and Vector classes were retrofitted to be part of this new Java Collections Framework and, as part of this, to implement interfaces from that framework. Hashtable was altered to implement Map and Vector was similarly altered to implement List.

Although Hashtable was retrofitted to implement Map and 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 Hashtable nor Vector could be changed to meet this convention. Most implementations in the Java Collections Framework (such as ArrayList, HashMap, and 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 ConcurrentHashMap, EnumMap, WeakHashMap.

So, other than name, what differences now exist between a Hashtable and a HashMap if 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 ArrayList and Vector.

ArrayList and HashMap can 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: Collections.synchronizedCollection(collectionOfYourChoice);.

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 Hashtable and HashMap.

jGuru: What are the differences between HashMap and Hashtable?
http://www.jguru.com/faq/view.jsp?EID=430247

Difference Between HashMap and HashTable
http://www.geekinterview.com/question_details/5126

Hashtable, HashMap, & Properties
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter10/hashtable.html

Java Hashmap or Hashtable
http://webjournl.wordpress.com/2007/03/08/java-hashmap-or-hashtable/

Hashtable or HashMap?
http://fitzwriter.wordpress.com/2008/01/17/hashtable-or-hashmap/

HashMap Versus Hashtable
http://twit88.com/blog/2007/12/16/java-hashmap-versus-hashtable/

Java HashMap Example
https://examples.javacodegeeks.com/hashmap-java-example/

2 comments:

MyTechTroubles said...

Nicely Summed up.

Thanks

javin paul said...

Here is what I have found some differences between hashtable and hashmap in java

1) hashtable is synchronized while hashmap is not.
2) hashmap is fast while hashtable is slow
3) hashtable is old but hashmap is new
4) hashtable supports enumeration while hashmap uses Iterator

5) hashmap allows null keys and values while hashtable doesn't allow null keys

Thanks
Javin
How HashMap internally works in Java