Java – HashMap versus Hashtable
By admin on Dec 16, 2007 in Java, Programming
This is one of the basic questions that is often asked by novice programmers.
In summary, both are more or less the same, except
- Access to the Hashtable is synchronized while access to the HashMap is not.
- Iterator in the HashMap is fail-safe while the enumerator for the Hashtable is not.
- HashMap permits null values in it, while Hashtable does not.
Hashtable extends Dictionary and implements Map.
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, java.io.Serializable {
HashMap extends AbstractMap and implements Map.
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {
Since both implement Map interface. You can do the followings…
Map h = new Hashtable(); Map m = new HashMap();
Sorry, comments for this entry are closed at this time.