3 Answers Found

I've often wondered this myself. First of all, HashMap is an implementation ofAbstractMap while Hashtable is an implementation of Dictionary. The majordifferences are really that you cannot use null values in Hashtable and alsothat HashMap is unsynchronized. Being unsynchronized means that a HashMap isnot thread-safe. So, in a Hashtable you can call the methods keys() orelements() and get an Enumeration() which is a thread-safe way to process allthe keys or elements respectively. HashMap does not give you this ability.

Hash table based implementation of the Map interface. This implementationprovides all of the optional map operations, and permits null values and thenull key. (The HashMap class is roughly equivalent to Hashtable, except that itis unsynchronized and permits nulls.) This class makes no guarantees as to theorder of the map; in particular, it does not guarantee that the order willremain constant over time.

This implementation provides constant-time performance for the basic operations(get and put), assuming the hash function disperses the elements properly amongthe buckets. Iteration over collection views requires time proportional to the"capacity" of the HashMap instance (the number of buckets) plus its size (thenumber of key-value mappings). Thus, it's very important not to set the initialcapacity too high (or the load factor too low) if iteration performance isimportant.

An instance of HashMap has two parameters that affect its performance: initialcapacity and load factor. The capacity is the number of buckets in the hashtable, and the initial capacity is simply the capacity at the time the hashtable is created. The load factor is a measure of how full the hash table isallowed to get before its capacity is automatically increased. When the numberof entries in the hash table exceeds the product of the load factor and thecurrent capacity, the capacity is roughly doubled by calling the rehash method.

Hashtable

This class implements a hashtable, which maps keys to values. Any non-nullobject can be used as a key or as a value.

To successfully store and retrieve objects from a hashtable, the objects used askeys must implement the hashCode method and the equals method.

An instance of Hashtable has two parameters that affect its performance: initialcapacity and load factor. The capacity is the number of buckets in the hashtable, and the initial capacity is simply the capacity at the time the hashtable is created. Note that the hash table is open: in the case of a "hashcollision", a single bucket stores multiple entries, which must be searchedsequentially. The load factor is a measure of how full the hash table is allowedto get before its capacity is automatically increased. When the number ofentries in the hashtable exceeds the product of the load factor and the currentcapacity, the capacity is increased by calling the rehash method.