The difference between HashMap, TreeMap and HashTable

https://www.cnblogs.com/sidekick/p/8010522.html

There are three important implementation classes for the Map interface, namely HashMap, TreeMap and HashTable.

TreeMap is ordered, HashMap and HashTable are disordered.

The method of Hashtable is synchronized, and the method of HashMap is not synchronized. This is the main difference between the two.

This means that Hashtable is thread-safe, HashMap is not thread-safe. HashMap is more efficient, while Hashtable is less efficient. If there is no requirement for synchronization or compatibility with legacy code, HashMap is recommended. Looking at the source code of Hashtable, you can find that, except for the constructor, all public method declarations of Hashtable have the synchronized keyword, while the source code of HashMap does not.

Hashtable does not allow null values, HashMap allows null values ​​(both key and value are allowed)

The parent class is different: the parent class of Hashtable is Dictionary, and the parent class of HashMap is AbstractMap

The default size of the hash array in Hashtable is 11, and the increase method is old*2+1.

The default size of the hash array in HashMap is 16, and it must be an exponent of 2.

Guess you like

Origin blog.csdn.net/weixin_43438052/article/details/113879644