开发者

Does Hashcode equality imply refer reference based equality?

开发者 https://www.devze.com 2023-03-01 14:48 出处:网络
I read that to use equals() method in java we also have to override the hashcode() method and that the 开发者_运维知识库equal (logically) objects should have eual hashcodes, but doesn\'t that imply re

I read that to use equals() method in java we also have to override the hashcode() method and that the 开发者_运维知识库equal (logically) objects should have eual hashcodes, but doesn't that imply reference based equality! Here is my code for overridden equals() method, how should I override hashcode method for this:

@Override    
public boolean equals(Object o)
        {
            if (!(o instanceof dummy))
                return false;
            dummy p = (dummy) o;
            return (p.getName() == this.getName() && p.getId() == this.getId() && p.getPassword() == this.getPassword());

        } 

I just trying to learn how it works, so there are only three fields, namely name , id and password , and just trying to compare two objects that I define in the main() thats all! I also need to know if it is always necessary to override hashcode() method along with equals() method?


Hashcode equality does not imply anything. However, hashcode inequality should imply that equals will yield false, and any two items that are equal should always have the same hashcode.

For this reason, it is always wise to override hashcode with equals, because a number of data structures rely on it.


Even though failure to override hashCode() will only break usage of your class in HashSet, HashMap, and other hashCode dependent structures, you should still override hashCode() to maintain the contract described by Object.

The general strategy of most hashCode() implementations is to combine the hash codes of the fields used to determine equality. In your case, a reasonable hashCode() may look something like this:

public int hashCode(){
    return this.getName().hashCode() ^ this.getId() ^ this.getPassword().hashCode();
}


You need to override hashCode() when you override equals(). Merely using equals() is not enough to require you to override hashCode().


In your code, you aren't actually comparing your fields' values. Use equals() instead of == to make your implementation of equal correct.

return (p.getName().equals(this.getName()) && ...

(Note that the above code can cause null reference exceptions if getName() returns null: you may want to use a utility class as described here)

And yes hashCode() would be called when you use some hashing data structure like HashMap,HashSet

You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.

from Effective Java, by Joshua Bloch

Also See

  • overriding-equals-and-hashcode-in-java
  • hashcode-and-equals
  • Nice article on equals() & hashCode()


The idea with hashCode() is that it is a unique representation of your object in a given space. Data structures that hold objects use hash codes to determine where to place objects. In Java, a HashSet for example uses the hash code of an object to determine which bucket that objects lies in, and then for all objects in that bucket, it uses equals() to determine whether it is a match.

If you don't override hashCode(), but do override equals(), then you will get to a point where you consider 2 objects to be equal, but Java collections don't see it the same way. This will lead to a lot of strange behaviour.

0

精彩评论

暂无评论...
验证码 换一张
取 消