I have been reading/researching the reason why HashMap
is faster than HashSet
.
I am not quite understanding the following statements:
HashMap
is faster thanHashSet
because the values are associated to a unique key.In
HashSet
, member object is used for calculating hashcode value which can be same for two objects soequals()
method is used to check for equality. If it returnsfalse
, that means the two objects are different. InHashMap
, the hashcode value is calculated using the key object.The
HashMap
hashcode value is calculated using the key object. Here, the member object is used to calculate the hashcode, which can be the same for two objects, soequals()
method is used to check for equality. If it returnsfalse
, that means the two objects are different.
To conclude my question:
I thought
HashMap
andHashSet
calculate the hashcode in the same way. Why are they different?Can you provide a concrete example how
HashSet
andHashMap
calculating the hashcode differently?I know what a "key object" is, but what does it mean by "member object"?
HashMap
can do the same things asHashSet
, and faster. Why do we needHashSet
? Example:HashMap <Object1, Boolean>= new HashMap<Object1, boolean>(); map.put("obj1",true); => exist map.get("obj1"); =>if null = not exist, else exist