1. The difference between HashMap and Hashtable
Let's first look at the definition of two classes
public class Hashtable extends Dictionary implements Map, Cloneable, java.io.Serializable
public class HashMap extends AbstractMap implements Map, Cloneable, Serializable
It can be seen that Hashtable inherits from Dictionary and HashMap inherits from AbstractMap
The put method of Hashtable is as follows
public synchronized V put(K key, V value) { //########## Note here 1 // Make sure the value is not null if (value == null) { //####### Note here 2 throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. Entry tab[] = table; int hash = key.hashCode(); //######## Note here 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 1 The method is synchronous
Note 2 method does not allow value==null
Note 3 The method calls the hashCode method of the key. If key==null, a null pointer exception will be thrown. The put method of HashMap is as follows.
public V put(K key, V value) { //########## Note here 1 if (key == null) //######### Note here 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 here return null; } Note 1 The method is asynchronous
Note 2 method allows key==null
Note 3 The method does not make any calls to the value, so it is allowed to be null.
Replenish:
Hashtable has a contains method, which can easily cause misunderstandings, so it has been removed in HashMap.
Of course, both classes use the containsKey and containsValue methods.
| HashMap | Hashtable | |
| Parent class | AbstractMap | Dictionary |
| Is it synchronized | no | yes |
| k, can v null | yes | no |
HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation). They all complete the Map interface. The main difference is that HashMap allows null keys. Due to non-thread-safe, it may be more efficient than Hashtable.
HashMap allows null as a key or value of an entry, while Hashtable does not.
HashMap removes the Hashtable contains method and changes it to containsvalue and containsKey. Because the contains method is easy to cause misunderstanding.
Hashtable inherits from the Dictionary class, and HashMap is an implementation of Map interface introduced by Java 1.2.
The biggest difference is that the Hashtable method is Synchronize, while HashMap is not. When multiple threads access Hashtable, they do not need to synchronize for its methods themselves, and HashMap must provide external synchronization (Collections.synchronizedMap).
The hash/rehash algorithms used by Hashtable and HashMap are roughly the same, so there will be no big difference in performance.
Summarize:
Key values in HashMap are allowed to be empty and are asynchronous
The key value in Hashtable is not allowed to be null and is synchronized
Inheritance is different, but both implement the Map interface
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.