The difference between hasttable and HashMap:
(1) Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2.
(2) This difference is the most important point: The method in Hashtable is synchronous, and the HashMap method (by default) is not synchronous. That is, in a multithreaded application, it is safe to use Hashtable without specialized operations, and for HashMap, an additional synchronization mechanism is required. However, the HashMap synchronization problem can be solved by a static method of collections:
Map Collections.synchronizedmap (map m)
This method returns a synchronized map that encapsulates all the methods of the underlying hashmap, making the underlying hashmap secure even in multi-threaded environments.
(3) Only HashMap can let you use NULL as the key or value of an entry for a table. Only one record in HashMap can be an empty key, but any number of entries can be empty value. This means that if a search key is not found in the table, or if a search key is found, but it is an empty value, then get () returns NULL. If necessary, use the Containkey () method to differentiate between the two cases.
Some other information:
1.HashTable method is synchronous, HashMap is not synchronized, so in multi-threaded occasions to manually synchronize HashMap this difference is like vector and ArrayList. (The main difference)
2.HashTable does not allow null values (both key and value are not allowed), HashMap allows null values (both key and value are allowed, only a key with a null value can be used, and multiple
The value of NULL values).
3.HashTable has a contains (object value) that functions like the Containsvalue (object value) function.
4.HashTable use Enumeration,hashmap with iterator.
The above is only the surface of the different, their implementation is very different.
The default size of the hash array in 5.HashTable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index.
6. Using different hash values, Hashtable directly uses the object's Hashcode, which is the code:
int hash = Key.hashcode ();
int index = (hash & 0x7FFFFFFF)% Tab.length;
Instead, HashMap recalculates the hash value and uses 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;
}
static int indexfor (int h, int length) {
Return H & (LENGTH-1);
}
These are just some of the more prominent differences, of course, their implementation is still a lot of different, such as
HashMap the operation to NULL.
Differences between HashMap and Hashtable and HashSet in Java