The ultimate summary of HashMap, HashSet, Hashtable key/value

Hash is involved, that is, the corresponding value is accessed through the hash value of the Key.

Note that the hash value of the same key must be unique and immutable.

 

1. HashMap, when the key is null, exists in tab[0].

The value can be empty, and if the hash of the key is different, more than one can be stored.

 

2. HashSet, using HashMap, add(value) uses the put(value, false) method of HashMap,

It is to store the value as the key of the HashMap. Null can be stored, only one can be stored.

 

3. Hashtable, multi-thread safety, synchronized modification, the bottom layer uses map.entry,

Source code: If the value is null, an exception is thrown. If the key is null, take its hash as an error.

Reason: Because it may involve multiple threads, if the value is null and another thread takes the value, it does not know whether it has been obtained or not.

 

Of course, another thread-safe CurrentHashMap, whose key or value is null, is not allowed. The reason is the same as Hashtable.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325306639&siteId=291194637