java container: 4, HashMap and HashTable difference?

HashMap and HashTable difference?

(1) thread-safe
HashTable are thread-safe. HashMap does not support simultaneous threads, so it is not thread safe. Under single-threaded operation, in terms of efficiency, HashMap may be higher than HashTable.

(2) key value whether to null
the HashMap is allowed up to a key value is null; HashTable, but is not allowed.

(. 3) contains a method
HashTable contains methods have similar containsValue ();
HashTable contains no method, but a containsValue () and containsKey () method.

(4) initial capacity and expansion mode

HashMap initial capacity is 16, and the capacity must be an integer power of two, the original expanded 2 times expansion;
initial capacity HashTable is 11, is 2 times the original expanded +1 expansion.

(4) selection and use
if no thread-safe, use HashMap, if need thread-safe, then use ConcurrentHashMap, ConcurrentHashMap not only thread-safe, the efficiency is higher than HashTable, HashTable has almost been eliminated.

Published 57 original articles · won praise 13 · views 1118

Guess you like

Origin blog.csdn.net/weixin_42924812/article/details/105054181