HashMap vs HashSet vs HashTable in java

... by Bittle in Programming Help January 11, 2019


  • Implements Map interface
  • uses put method to store key-value pairs
  • Doesn't allow duplicate keys but values can be duplicated
  • Maximum of one NULL key but any number of NULL values
  • Internally uses an array


  • Implements Set interface
  • uses add method to store only unique objects
  • Doesn't allow duplicate objects
  • Allows maximum of one NULL object
  • Internally uses HashMap to store unique objects


  • Synchronized
  • Considered legacy code
  • Doesn't allow NULL keys or values

Whatever can be done in HashTable can be done with a HashMap, so stick to HashMaps.

When to use HashMap?

  • Synchronization is not needed
  • Key value pairs are wanted
  • Search operation on multiple threads

When to use HashSet?

  • Order is not required
  • Mathematical sets (ex: {1, 2, 3, 4})
  • Don't want duplicate keys

Comments (0)

Search Here