Java – how does HashSet handle hashcode()?

I'm trying to get a deeper understanding of Java util. Collection and Java util. Map, but I have some questions about the HashSet function:

In the document, it says: this class implements the set interface and is supported by a hash table (actually a HashMap instance) OK, so I can see that HashSet always runs hashtable in the background A hash table is a structure that requests keys and values every time a new element is added to it Then, the value and key are stored in the bucket based on the key hashcode If the hash codes of the two keys are the same, the linked list is used to add the two key values to the same bucket If I am wrong, please correct me

So, my question is: if a HashSet always has a hashtable running in the background, then every time we use HashSet When the add () method adds a new element to the HashSet, the HashSet should add it to its internal hashtable However, hashtable requires a value and a key, so what key does it use? Does it just use the value we're trying to add as a key and then use its hashcode? If I am wrong about the HashSet implementation, please correct me

My other question is: in general, which classes can use the hashcode () method of Java objects? I ask this because, in the document, it says that we need to override the hashcode () method every time we override the equals () method Well, this really makes sense, but I doubt if it's just a suggestion that all we should do is keep everything "beautiful and perfect" (in this way), or if it's really necessary, because many Java default classes may continue to use the object's hashcode () method In my view, I can't see other classes that use this method instead of collection related classes Thank you very much.

Solution

If you look at the actual Javacode of HashSet, you can see its role:

// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
...

 public boolean add(E e) {
    return map.put(e,PRESENT)==null;
}

Therefore, the element you want to add supports the key in HashMap, and its value is a virtual value HashSet has never actually used this virtual value

The second question about overriding equals and hashcode:

If you want to override either, you must always override both This is because the hashcode contract states that equal objects must have the same hash code The default implementation of hashcode will provide different values for each instance

Therefore, this can happen if you override equals () but not hashcode ()

object1.equals(object2) //true

MySet.add(object1);

MySet.contains(object2); //false but should be true if we overrode hashcode()

Because contains will use the hash code to find the bucket to search, we may get different buckets instead of finding the same object

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>