Differences between hashtable and HashMap

① Inheritance is different.

The methods in hashtable are synchronous, while the methods in HashMap are asynchronous by default. In the multithreaded concurrent environment, you can directly use hashtable, but if you want to use HashMap, you need to add synchronization processing yourself.

In hashtable, null values are not allowed for both key and value.

In HashMap, null can be used as a key, and there is only one such key; One or more keys can have null values. When the get () method returns a null value, it can mean that the key does not exist in the HashMap or that the value corresponding to the key is null. Therefore, in the HashMap, the get () method cannot be used to judge whether there is a key in the HashMap, but the containskey () method should be used to judge.

④ The internal implementation of the two traversal methods is different.

Both hashtable and HashMap use iterator. For historical reasons, hashtable also uses the enumeration method.

Hash values are used differently. Hashtable directly uses the hashcode of the object. HashMap recalculates the hash value.

Hashtable and HashMap are the initial size and capacity expansion methods of the two internal implementation arrays. The default size of hash array in hashtable is 11, and the increase method is old * 2 + 1. The default size of hash array in HashMap is 16, and it must be the exponent of 2

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>