I have been reading/researching the reason why HashMapis faster than HashSet.
I am not quite understanding the following statements:
HashMapis faster thanHashSetbecause 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
HashMaphashcode 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
HashMapandHashSetcalculate the hashcode in the same way. Why are they different?Can you provide a concrete example how
HashSetandHashMapcalculating the hashcode differently?I know what a "key object" is, but what does it mean by "member object"?
HashMapcan 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