Question:

What differences exist between HashMap and Hashtable ?

Answer:

Both the HashMap and Hashtable classes implement the Map interface and thus, have very similar characteristics. However, they differ in the following features:

  • HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow neither null keys, nor null values.
  • Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a Hashtable is suitable for multi-threaded environments.
  • HashMap provides its set of keys and a Java application can iterate over them. Thus, a HashMap is fail-fast. On the other hand, a Hashtable provides an Enumeration of its keys.
  • The Hashtable class is considered to be a legacy class.

Keywords:

© 2017 QuizBucket.org