What is the difference between HashMap and Hashtable? hashmaphashtable
Differences between HashMap and Hashtable
1. Different inherited parent classes:
The parent class of HashTable is Dictionary.
The parent class of HashMap is AbstractMap;
2. thread security:
HashTable: thread security. The HashTable method is modified by synchronized.
HashMap: thread security
3. contains ()
HashTable: Reserved contains (), containsKey (), containsValue ()
HashMap: contains () is removed, and containsKey () and containsValue () are retained ()
4. Whether the key and value can be null
HashTable: key and value cannot be empty. the reason is that the method in the source code will traverse the entry and call equals () with the key or value of the entry. Therefore, you must first determine whether the key/value is null. If it is null, an exception will be thrown.
HashMap: key, value can be null. null can be used as only one key, and null can be used as value to have multiple
5. Different Internal implementations of traversal methods
HashTable: Use Enumeration, Iterator
HashMap: Use Iterator
6. the array initialization and expansion modes used by the internal implementation are different.
HashTable: the initial size of the array is 11, and the expansion mode is 2 * old + 1
HashMap: the exponential power of the array with the initial size of 16 and the expansion method of 2
7. Different hash values
HashTable: directly use hashcode ()
HashMap: Re-calculate the hash value
Only list the differences. The detailed differences will be added slowly...