HashMap vs Hashtable

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


22 points

what is the difference between HashMap vs Hashtable Which is best to Use



4 points

Hi,
They are almost the same thing. The big differences are that HashMap is the implementation of a Hashtable on a Map interface, is not synchronized, and it allows null values for key and value, while Hashtable doesn't.

Alessandro A. Garbagnati

Anonymous's picture
Created by Anonymous
1 point

Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2.

The key differnce between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default.

Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know.

-John Zukowsk

Anonymous's picture
Created by Anonymous
0 points

1. The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn't allow nulls).
2. HashMap does not guarantee that the order of the map will remain constant over time.
3. HashMap is non synchronized whereas Hashtable is synchronized.
4. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't

to read more about Hashtable vs HashMap See here.

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.