-
Inherit different.
public class Hashtable extends Dictionary implements Map
public class HashMap extends AbstractMap implements Map
-
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.
-
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.
-
Internal traversal implementation different.
Hashtable, HashMap use the Iterator. And for historical reasons, Hashtable also used the Enumeration way.
-
Different hash values, HashTable object directly hashCode. The HashMap recalculate the hash value.
-
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.
-
HashMap contains no method, only containsValue and containsKey. Hashtable has three.
Java HashMap and Hashtable difference
Guess you like
Origin blog.csdn.net/siriusol/article/details/105044815
Recommended
Ranking