1. HashMap 比 HashSet 更快,因为值与唯一键相关联。 2. 在 HashSet 中,成员对象用于计算哈希码值,可能相同,因此使用 equals() 方法检查相等性。如果返回 false,则表示两个对象不同。在 HashMap 中,哈希码值使用键对象计算。 3. HashMap 的哈希码值是使用键对象计算的。在这里,成员对象用于计算哈希码,可能对于两个对象相同,因此使用 equals() 方法来检查相等性。如果返回 false,则表示两个对象不同。
总之我的问题是:
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