The difference between hashtabel and hashmap


1.Hashtable is thread-synchronized and thread-safe. Hashmaps are not thread-synchronized, so they are not thread-safe. So in multi-threaded situations, the difference between manually synchronizing the hashmap is like 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 function as containsvalue(object value).
4.Hashtable uses enumeration, and hashmap uses iterator.
The above are only superficial differences, and their implementations are also very different.
5. The default size of the hash array in the hashtable is 11, and the way to increase it is old*2+1. The default size of the hash array in the hashmap is 16, and it must be an index of 2.
6. The use of hash values ​​is different. The hashtable directly uses the hashcode of the object. The code is as follows:
int hash = key.hashcode();
int index = (hash & 0x7fffffff) % tab.length;
and the hashmap recalculates the hash value, And use AND instead of 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;
}
static int indexfor(int h, int length) {
return h & (length-1);
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326781083&siteId=291194637