Hashtable source code implementation and HashMap is basically no big difference, you can see the HashMap chapter for more information.
The point to summarize
1.Hashtable does not support null keys or values. HashMap and LinkMap support:
Null throws a NullPointerException (null); null throws a NullPointerException (null); ``` public synchronized V put(K key, V value) {// Make sure the value is not null if (value == null) {// Throw a NullPointerException exception NullPointerException(); } // Makes sure the key is not already in the hashtable. HashtableEntry<? ,? > tab[] = table; int hash = key.hashCode(); Int index = (hash & 0x7FfffF) % tab.length; // If key is null, NullPointerException is thrown. @SuppressWarnings("unchecked") HashtableEntry<K,V> entry = (HashtableEntry<K,V>)tab[index]; for(; entry ! = null ; entry = entry.next) { if ((entry.hash == hash) && entry.key.equals(key)) { V old = entry.value; entry.value = value; return old; } } addEntry(hash, key, value, index); return null; } ' 'and HashMap is fault-tolerant for NULL: Public V put(K key, V value) {return putVal(hash(key), key, value, false, true); } static final int hash(Object key) { int h; Return (key == null)? 0 : (h = key.hashCode()) ^ (h >>> 16); } ` ` `Copy the code
2.Hashtable is thread-safe, HashMap and LinkHashMap are not thread-safe:
Hashtable uses synchronized on key interfaces such as data access to achieve thread synchronization:
public synchronized V get(Object key){... } public synchronized V put(K key, V value) {... } public synchronized V put(K key, V value){... } public synchronized void putAll(Map<? extends K, ? extends V> t) {... } public synchronized V remove(Object key){... } public synchronized void clear(){... } public synchronized boolean contains(Object value){... } public synchronized boolean containsKey(Object key){... }...Copy the code
A bunch of interfaces for writing and reading data are simply synchronized,
3. In terms of the processing efficiency of multi-thread concurrency, Hashtable synchronized mechanism is much lower than HashMap and LinkHashMap, but at least it ensures thread safety and there is no need to manually increase thread-safety operations
4.Hashtable itself is becoming obsolete and is not recommended.
* If a thread-safe implementation is not needed, it is recommended to use
* {@link HashMap} in place of {@code Hashtable}. If a thread-safe
* highly-concurrent implementation is desired, then it is recommended
* to use {@link java.util.concurrent.ConcurrentHashMap} in place of
* {@code Hashtable}.
Copy the code
If thread safety is not required, use something like HashMap. Use ConcurrentHashMap if you need higher concurrent thread safety requirements, which are much more efficient than Hashtable.