1. Hashtable is a subclass of Dictionary.
Copy the code code as follows:
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable
HashMap:
Copy the code code as follows:
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
HashMap and Hashtable are both implementation classes of the Map interface;
2. The methods in Hashtable are synchronous (), but the methods in HashMap are not synchronous by default. That is to say, in multi-threaded applications, Hashtable can be used safely without special operations; for HashMap, additional synchronization mechanism is required. But the synchronization problem of HashMap can be solved through a static method of Collections:
Copy the code code as follows:
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
This method returns a synchronized Map, which means that the returned Map is thread-safe. It should be noted that when iterating over the returned map, you must manually synchronize on the returned map, otherwise it will lead to undefined behavior:
Copy the code code as follows:
Map m = Collections.synchronizedMap(new HashMap());
...
Set s = m.keySet(); // Needn't be in synchronized block
...
synchronized(m) { // Synchronizing on m, not s!
Iterator i = s.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
3. In HashMap, null can be used as a key, and there is only one such key; there can be one or more keys whose corresponding value is null. When the get() method returns a null value, it can mean that the key does not exist in the HashMap, or it can also mean that the value corresponding to the key is null. Therefore, in HashMap, the get() method cannot be used to determine whether a certain key exists in the HashMap, but the containsKey() method should be used to determine. The key value of Hashtable cannot be null, otherwise: java.lang.NullPointerException.
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 HashTable is 11, and the increasing method is old*2+1. The default size of the hash array in HashMap is 16, and it must be an exponent of 2.
6. The use of hash values is different. HashTable directly uses the hashCode of the object. The code is as follows:
Copy the code code as follows:
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFFF) % tab.length;
HashMap recalculates the hash value and uses AND instead of modulus, such as the put method of HashMap:
Copy the code code as follows:
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
Copy the code code as follows:
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
Copy the code code as follows:
static int indexFor(int h, int length) {
return h & (length-1);
}