Java interview questions-difference between HashMap and Hashtable, hashmaphashtable
I. Differences between HashMap and Hashtable
Let's first look at the definitions of the two classes.
1 public class Hashtable 2 extends Dictionary 3 implements Map, Cloneable, java.io.Serializable
public class HashMap extends AbstractMap implements Map, Cloneable, Serializable
It can be seen that Hashtable inherits from Dictiionary, while HashMap inherits from AbstractMap.
The put Method of Hashtable is as follows:
Public synchronized V put (K key, V value) {// ###### Note: 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 that value = null is not allowed in method 2.
Note 3: The hashCode method of key is called. If key = null, a null pointer exception is 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 that return null here ;}
NOTE 1: The method is not synchronous.
NOTE 2: key = null is allowed.
Note that the 3 method does not call the value, so the value can be null.
Supplement:
Hashtable has a contains method, which may cause misunderstanding. Therefore, it has been removed from HashMap.
Of course, both classes use the containsKey and containsValue methods.
HashMap Hashtable
Parent class AbstractMap Dictiionary
Synchronous? No Yes
Can k and v be null? 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 key values. Because of non-thread security, the efficiency may be higher than Hashtable.
HashMap allows null as the key or value of an entry, whereas Hashtable does not.
HashMap removes the contains method of Hashtable and changes it to containsvalue and containsKey. The contains method is easy to misunderstand.
Hashtable inherits from the Dictionary class, while HashMap is an implementation of the Map interface introduced by Java1.2.
The biggest difference is that the Hashtable method is Synchronize, but HashMap is not. When multiple threads access Hashtable, they do not need to implement synchronization for their own methods, hashMap must provide external synchronization (Collections. synchronizedMap ).
The hash/rehash algorithms used by Hashtable and HashMap are roughly the same, so there is no big difference in performance.
Summary:
HashMapKeyAllowed to be emptyAnd yesNon-synchronousOf
HashtableKeyCannot be blankYesSynchronizationOf
Different inheritance methods, but all implement the Map interface