It depends on whether you need to search the data structure based on the key alone or both the key and the value.
If you search by the key alone (i.e. map.containsKey(key)
), you should use a HashMap
.
If you search for existence of a key-value pair (i.e. set.contains(new Pair(key,value)
), you should use a HashSet
that contains those pairs.
Another thing to consider is how you determine the uniqueness of the elements. If it is determined by the key alone, you should use a HashMap
. If it is determined by both key and value (i.e. you can have the same key appear twice with different values), you must use HashSet
, since HashhMap
doesn't allow the same key to appear more than once.