Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashcode and equals

Tags:

java

hashcode

equals and hashCode method must be consistent, which means that when two objects are equal according to equals method their hashCode method should return the same hash value.

Java returns a unique hash code if we do not override the hashCode() method.

class HashValue {

    int x;

    public boolean equals(Object oo) {
        // if(oo instanceof Hashvalue) uncommenting ths gives error.dunno why?
        // :|
        HashValue hh = (HashValue) oo;

        if (this.x == hh.x)
            return true;
        else
            return false;
    }

    HashValue() {
        x = 11;
    }

}

class Hashing {
    public static void main(String args[]) {
        HashValue hv = new HashValue();
        HashValue hv2 = new HashValue();

        System.out.println(hv.hashCode());
        System.out.println(hv2.hashCode());

        if (hv.equals(hv2))
            System.out.println("EQUAL");
        else
            System.out.println("NOT EQUAL");
    }
}

Why does uncommenting the line gives compilation error?

If the objects have unequal hash codes, why are they shown equal even though the default hashcode varies?

like image 828
Nitish Upreti Avatar asked Jan 02 '10 05:01

Nitish Upreti


People also ask

What is the difference between hashCode and equals?

The key difference between equals and hashCode in Java is that the equals is used to compare two objects while the hashCode is used in hashing to decide which group an object should be categorized into.

What is the relationship between hashCode () and equals () method in Java?

The hashCode() method should return the same integer value for the same object for each calling of this method unless the value stored in the object is modified. If two objects are equal(according to equals() method) then the hashCode() method should return the same integer value for both the objects.

Why should we override hashCode and equals method?

Case 1: Overriding both equals(Object) and hashCode() method Whenever it(hashcode) is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.

What happens if you override equals but not hashCode?

Overriding only equals() method without overriding hashCode() causes the two equal instances to have unequal hash codes, which violates the hashCode contract (mentioned in Javadoc) that clearly says, if two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two ...


1 Answers

Equality is only determined by method equals(). And method hashCode() is used in other situations, like by Map or Set. It is somewhat like a pre-condition or hint before actually calling equals (for efficiency). So it is assumed that if 2 objects are equal (that is, equals() returns true), then their hashCodes() must return the same value.

So in your code, 2 objects are equal, as long as your overriden equals() returns true, no matter what hashCode() does. hashCode() is not called at all when comparing for equality.

This question has more in-depth information regarding to the relationship between equals() and hashCode().

like image 117
bryantsai Avatar answered Sep 20 '22 16:09

bryantsai