Analyze the detailed difference between HashMap and HashTable from the source code

to sum up:

  1. The inherited parent class is different: HashTable inherits from Dictionary class, while HashMap inherits from AbstractMap class. But both implement the Map interface

  2. Thread safety: HashMap is not thread-safe, while HashTable is thread-safe. Through source code analysis, we know that every method of HashTable is synchronized.

  3. Whether key and value allow null values: HashMap can store key-value pairs with key=null, HashTable cannot store key-value pairs with key=null

  4. The hash value is different: HashTable directly uses the hashCode of the object. And HashMap recalculates the hash value

  5. The default initial capacity and how to expand are different: the default initial capacity of HashMap is 16, and the default initial capacity of HashTable is 11; the load factor of HashMap and HashTable are both 0.75; HashMap expansion: 2 N, and HashTable expansion: 2 N+1.

Source code analysis
Insert picture description here

1. The inherited parent class is different

HashMap

Insert picture description here

HashTableInsert picture description here

2. Thread safety

PUT method of HashMapInsert picture description here

PUT method of HashTableInsert picture description here

In this way, we can see that HashMap is not thread-safe, so the difference between them is thread-safe.

3. Whether the key and value allow null values

HashMap's containsValue method source code

Insert picture description here

HashTable's containsValue method source code

Insert picture description here

We can clearly see from the above ContainsKey method and the source code of ContainsValue:

  • Neither key nor value is allowed to appear in Hashtable. But if there is an operation similar to put(null,null) in Hashtable, the compilation can also pass, because the key and value are of Object type, but the NullPointerException will be thrown at runtime, which is stipulated by the JDK specification;
  • In HashMap, null can be used as a key, and there is only one such key; there can be one or more keys corresponding to the value of null. When the get() method returns a null value, it may be that the key does not exist in the HashMap, or the value corresponding to the key may be null. Therefore, in HashMap, the get() method cannot be used to determine whether a key exists in the HashMap, but the containsKey() method should be used to determine.

4. The hash value is different

The use of hash value is different, HashTable directly uses the hashCode of the object. And HashMap recalculates the hash value
hashCode is a value of type int calculated by jdk based on the address or string or number of the object

HashMap

put method

Insert picture description here

hash methodInsert picture description here

HashTable

Insert picture description here

Hashtable calculates the hash value by directly using the hashCode() of the key, and HashMap recalculates the hash value of the key. Hashtable uses the modulus operation when calculating the position index corresponding to the hash value, while the HashMap uses the and Operation, and here generally use hash&0x7FFFFFFF first, and then take the modulus of length. The purpose of &0x7FFFFFFF is to convert a negative hash value into a positive value, because the hash value may be a negative number, and after &0x7FFFFFFF, only the outside of the symbol changes, and the latter The bits are unchanged.

5. The default initial capacity and how to expand are different

HashTable

Insert picture description here
Insert picture description here

HashMapInsert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44723496/article/details/112440968