java explain the difference between HashMap and Hashtable
- 2020-05-17 05:37:25
- OfStack
1. Difference between HashMap and Hashtable
Let's look at the definition of two classes first
public class Hashtable
extends Dictionary
implements Map, Cloneable, java.io.Serializable
public class HashMap
extends AbstractMap
implements Map, Cloneable, Serializable
You can see that Hashtable inherits from Dictiionary and HashMap inherits from AbstractMap
The put method for Hashtable is as follows
public synchronized V put(K key, V value) { //###### Note that there 1
// Make sure the value is not null
if (value == null) { //###### Note that there 2
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry tab[] = table;
int hash = key.hashCode(); //###### Note that there 3
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
V old = e.value;
e.value = value;
return old;
}
}
modCount++;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
Entry e = tab[index];
tab[index] = new Entry(hash, key, value, e);
count++;
return null;
}
Note that the 1 method is synchronous
Note that the 2 method does not allow value==null
Note that the 3 method calls the hashCode method of key, and if key==null, the put method of HashMap will throw a null pointer exception as follows
public V put(K key, V value) { //###### Note that there 1
if (key == null) //###### Note that there 2
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry 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); //###### Note that there
return null;
}
Note that the 1 method is asynchronous
Note that the 2 method allows key==null
Note that the 3 method does not make any calls to value, so null is allowed
Supplement:
Hashtable has one contains method, which is easy to cause misunderstanding, so it has been removed from HashMap
Of course, both classes use the containsKey and containsValue methods.
HashMap | Hashtable | |
父类 | AbstractMap | Dictiionary |
是否同步 | 否 | 是 |
k,v可否null | 是 | 否 |
HashMap is a lightweight implementation of Hashtable (a non-thread-safe implementation), and they all complete the Map interface. The main difference is that HashMap allows null (null) key values (key), which can be more efficient than Hashtable due to non-thread-safe.
HashMap allows null to be key or value of one entry, while Hashtable does not.
HashMap dropped the contains method of Hashtable and changed it to containsvalue and containsKey. Because the contains method is misleading.
Hashtable inherits from the Dictionary class, and HashMap is an implementation of Map interface introduced by Java1.2.
The biggest difference is that Hashtable's method is Synchronize, while HashMap is not. Instead of synchronizing its methods themselves when multiple threads access Hashtable, HashMap must provide external synchronization (Collections.synchronizedMap).
The hash/rehash algorithms used by Hashtable and HashMap are about the same, so there is not much difference in performance.
Conclusion:
Key values in HashMap are allowed to be null and unsynchronized
Key values in Hashtable are not allowed to be null and synchronized
Inheritance is different, but both implement the Map interface