Welcome to Srini's blog

Sunday, April 4, 2010

Hashtable Vs Hashmap -- Java Collection framework

1. Hashtable and Hashmap both provide key-value access to data.
2. The key difference between the two is that access to the Hashtable is synchronized while Hashmap is not synchronized. Hashtable performs better when you use a sequence of numbers as keys but can perform quite bad if the keys follow some special patterns while HashMap has an overall better performance in the real word and doesn't have "any" problems with special patterns.
3. The Hashtable is not part of the Java Collections Framework. But it does support all the API that a Collection interface specifies. Hashtable is what the Sun calls, the legacy collection implementation. Hashtable inherits from the Dictionary class which is obsolete in Java 1.6, while the HashMap inherits from collection interface “Map”.
4. Iterator in the HashMap is fail-safe where Hashtbale is not.
5. Hashtable is thread-safe. However there is a way of making Hashmaps thread safe using Collections.synchronizedMap(new Hashmap()).
6. HashMap permits one null key & more null values where Hashtable not permits null keys and values.

1 comment:

  1. here are some of 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

    Javin
    How HashMap internally works in Java

    ReplyDelete