Hashtable is widely used. hashmap is a class used to replace hashtable in the new framework. In other words, hashmap is recommended instead of hashtable. Maybe you think hashtable is very useful. Why not? Here we will briefly analyze their differences.
1. The hashtable method is synchronous, And the hashmap is not synchronized. Therefore, the difference between manual synchronization of hashmap in multithreading is like that of vector and arraylist.
2. hashtable does not allow null values (neither key nor value). hashmap allows null values (both key and value ).
3. hashtable has a contains (object value), which has the same functions as containsvalue (object value.
4. hashtable uses enumeration and hashmap uses iterator.
The above is just a difference on the surface, and their implementation is also very different.
5. The default size of the hash array in hashtable is 11, and the increase is in the old * 2 + 1 mode. The default size of the hash array in hashmap is 16, and it must be an index of 2.
6. For different hash values, hashtable directly uses the hashcode of the object,CodeYes:
Int hash = key. hashcode ();
Int Index = (hash & 0x7fffffff) % tab. length;
Hashmap recalculates the hash value and replaces the modulo:
Int hash = hash (k );
Int I = indexfor (hash, table. Length );
Static int Hash (Object X ){
Int H = x. hashcode ();
H + = ~ (H <9);
H ^ = (h> 14);
H + = (H <4 );
H ^ = (H >>> 10);
return h;
}< br> static int indexfor (int h, int length) {
return H & (length-1);
}< br> the above are some outstanding differences. Of course, there are many differences in their implementation, for example,
hashmap performs null operations.