Thursday, 24 January 2019

What differences exist between HashMap and Hashtable

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:

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

No comments:

Post a Comment