The difference between HashTable and HashMap

xiaoxiao2021-03-06  53

Hashtable is very wide, Hashmap is a class used in the new framework that replaces the HashTable, that is, it is recommended to use HashMap, do not use HashTable. Maybe you think is Hashtable is very easy to use, why don't you use it? It is simply analyzed their differences here.

1.Hashtable method is synchronized, and HashMap does not synchronize, so you have to manually synchronize the difference in Multi-threaded HashMap is like vector and arraylist.

2.Hashtable does not allow NULL values ​​(Key and Value can not be), HashMap allows NULL values ​​(Key and Value can be).

3.Hashtable has a Contains (Object Value), the function and the ContainSValue feature.

4.Hashtable uses ENUMERATION, Hashmap uses Iterator.

The above is only the surface of the surface, and their implementation is very different.

5. Hashtable Default size is 11, the increased method is OLD * 2 1. The default size of the HASH array in HashMap is 16, and must be 2 index.

6. The use of the hash value, the HashTable directly uses the object's hashcode, the code is like this: int has = key.hashcode (); int index = (hash & 0x7fffffff)% Tab.Length; HashMap recalculate the Hash value. And use with instead of demodulation: int has = 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);} The above is just some of the more prominent differences, of course, their implementation still has a lot of different, such as HashMap to NULL operations.

转载请注明原文地址:https://www.9cbs.com/read-114505.html

New Post(0)