Java HashMap and Hashtable difference

  1. Inherit different.

    public class Hashtable extends Dictionary implements Map

    public class HashMap extends AbstractMap implements Map

  2. The method of the Hashtable are synchronized, and a method of HashMap default is asynchronous. In multi-threaded environment, you can directly use the Hashtable, but if you want to use HashMap will have to increase their own synchronization. Hashtable methods are thread-safe, and HashMap is not thread safe.

  3. The Hashtable, key and value do not allow null values occur.

    The HashMap, as null key, such keys only one ; there may be one or more keys corresponding to the value of null. When the get () method returns a null value, may be the key does not represent a HashMap, it indicates that the bond may be the corresponding value of null. Thus, in the HashMap is not a get () method to determine whether a key is present in the HashMap , but should containsKey () method to determine.

  4. Internal traversal implementation different.

    Hashtable, HashMap use the Iterator. And for historical reasons, Hashtable also used the Enumeration way.

  5. Different hash values, HashTable object directly hashCode. The HashMap recalculate the hash value.

  6. Sizes and different initial expansion thereof manner Hashtable and HashMap two way arrays of internal implementation. HashTable the hash array 11 is the default size, increasing manner is old * 2 + 1. The default size of the HashMap hash array 16, and must be a power of two.

  7. HashMap contains no method, only containsValue and containsKey. Hashtable has three.

Published 42 original articles · won praise 86 · views 7028

Guess you like

Origin blog.csdn.net/siriusol/article/details/105044815