The HashSet
will be more memory-efficient and time-efficient, but leaves some ambiguity depending on the application.
Consider the scenario where a program processes many custom objects of some type User
, and records their answer to a "yes or no" question. There are 3 possible states a User
could be in during this processing:
User
says "yes"User
says "no"User
has not been processed yet
Using a HashSet
alone (i.e. without an additional data structure), depending on the situation it may be ambiguous as to whether or not a User
not found in the HashSet
has replied "no", or simply not been processed yet. A HashMap
, although being less efficient because it must store multiple Boolean
instances, would allow you to differentiate between the 3 cases listed above.
Note that in practicality, there are many cases where the 3rd case can be eliminated (e.g. by iterating through every User
instance so you can assume each processed User
is only encountered once), and the HashSet
would be the appropriate choice.