Home > Back-end >  The difference between a HashMap and Hashtable
The difference between a HashMap and Hashtable

Time:10-18

So many bosses, explain the problem too long at reading, briefly summarize yourself!
1, a HashMap is thread safe, will easily produce infinite loop in a multithreaded environment, but in a single-threaded environment operation with high efficiency; Hashtable thread-safe, many methods have synchronized, but at the same time because the locking leads to low efficiency, single thread environment
2, a HashMap allows a key is null, allows multiple value is null. The Hashtable does not allow the key or the value is null
3, the length of the array of different bottom HashMap the bottom of the length of the array must be 2 ^ n, the advantage is to prepare for later the hash algorithm, and the Hashtable underlying the length of the array can be any value, it is caused when the underlying array length as composite, Hashtable scattering is uneven, the hash algorithm easy to generate the hash conflict,
4, hash algorithm different Hashtable hash algorithm firstly makes a hash value less than or equal to the maximum value of an integer, then through % operation to achieve uniform scattering,
Because the computer is at the bottom of the operation is based on the 2 into the system, so the HashMap hash algorithm to use & amp; Operations to replace % operations, on the speed obviously HashMap hash algorithm better,
Mechanism of expanding capacity of 5, there's a difference: HashMap array of expansion of the whole idea is to create a length is 2 times the original array, and then traverse on the original array and copy
Hashtable the expansion will create a first length is 2 times the original length of the array, then use the head interpolation will list in reverse order
6, structure distinguishing : HashMap jdk1.8 in the original array + chain table structure is optimized, will implement the structure into an array + list + the red-black tree, in a linked list node element is greater than the 8 will list in red and black tree
Hashtable arrived jdk1.8 internal structure is not material optimization, continue to use array + list
  • Related