Which is true about HashMap and Hashtable?
-
Both HashMap and Hashtable will allow null as key and value.
-
HashMap is not Synchronized and Hashtable is Synchronized.
-
The iterator in the HashMap is fail-safe, but enumerator for the Hashtable is not fail-safe.
-
Hashtable will be faster than HashMap.
HashMap is unsynchronized (not thread-safe) while Hashtable is synchronized (thread-safe), which is the key architectural difference. HashMap allows one null key and multiple null values, but Hashtable does not allow any nulls. Hashtable is slower due to synchronization overhead. HashMap's iterator is fail-fast (not fail-safe) - it throws ConcurrentModificationException if modified during iteration.
The correct true statement is that HashMap is not synchronized while Hashtable is synchronized. HashMap also permits one null key and null values, whereas Hashtable permits neither, so the 'both allow null' option is false. The fail-safe option is wrong because HashMap's iterator is actually fail-FAST (it throws ConcurrentModificationException on structural modification), while Hashtable's enumerator is not fail-fast. HashMap is generally faster than Hashtable, not the reverse.