• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between hashtable and hash map  RSS feed

 
Pushker Chaubey
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why was HashMap introduced when we already had Hashtable. What are differences between the two?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lookup the description of Hashtable and HashMap in the API documentation.

The main difference is that Hashtable is synchronized and HashMap is not. Because of that, HashMap might be slightly faster, because it doesn't have the overhead of synchronization.

Hashtable is an old class from before the time of Java 1.2. In Java 1.2, a new collections API was added. See: The Collections Framework, especially the Design FAQ might be interesting if you want to know why it was designed as it is.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the synchronized part, one other difference is mentioned in the Javadocs of HashMap. Here's an extract:

The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.

[ December 11, 2006: Message edited by: Jaikiran Pai ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Neerav Narielwala
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hashmap is not synchronised, and allows null for key and values where as Hashtable is synchronised and does not allow null values. The elements may not be in an order in the case of hashmap.
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The elements may not be in an order in the case of hashmap


Even with HashTable, ordering and sorting is not guaranteed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!