Posts

Showing posts from November, 2019

What's the difference between HashMap and Hashtable?

Yes, there is a Hashtable class in Java. Right, Hashtable with a lowercase t. 1. First difference. They inherit from different classes.  Hashtable is based on Dictionary class. HashMap is implemented based on the Map interface since Java 1.2. See the code below. public class HashMap<K, V> extends AbstractMap<K, V> implements Cloneable, Serializable {...} public class Hashtable<K, V> extends Dictionary<K, V> implements Map<K, V>, Cloneable, Serializable {...} HashMap   extends the abstract class AbstractMap and implement the  Map interface: public abstract class AbstractMap<K, V> implements Map<K, V> {...} 2. The methods in Hashtable are synchronized,but the methods in HashMap are not synchronized by default. In multi-thread situations,we can use Hashtable.  If we choose to use HashMap, we need to handle the synchronization. 3. In Hashtable, no null value for keys ...