1

We all know that if equals method returns true, then two objects are equal.

Can anybody give an example where 2 objects have the same hash value but they are actually different?

3
  • Which language? Also, since most language's hash code implementation is free to change, one would either have to define a new data type and its hashing procedure, or assume a specific implementation and version. Commented Mar 10, 2014 at 18:19
  • m asking this in reference to java language. Commented Mar 10, 2014 at 18:23
  • I am asking this in reference to java Commented Mar 10, 2014 at 18:32

3 Answers 3

1

I'm assuming you're familiar with the contract(s) associated with overriding equals() and hashCode(), and the implications of a collision-prone hashCode implementation. Given that, the following trivial example uses an object that holds two Integers and implements a very simple hashCode, and demonstrates how easy it is to have two objects that aren't equal but have the same hashCode. Providing a more sophisticated hashCode algorithm can alleviate this.

The output of running main is:

hashCodes: ih1: 6, ih2: 6
equals: false

Example code:

package example.stackoverflow;

public class IntHolder
{
    private Integer primaryData;
    private Integer secondaryData;

    public IntHolder(Integer primaryData, Integer secondaryData)
    {
        this.primaryData = primaryData;
        this.secondaryData = secondaryData;
    }

    @Override
    public int hashCode()
    {
        return ((primaryData == null) ? 0 : primaryData.hashCode()) + 
               ((secondaryData == null) ? 0 : secondaryData.hashCode());
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        IntHolder other = (IntHolder) obj;
        if (primaryData == null)
        {
            if (other.primaryData != null)
                return false;
        }
        else if (!primaryData.equals(other.primaryData))
            return false;
        if (secondaryData == null)
        {
            if (other.secondaryData != null)
                return false;
        }
        else if (!secondaryData.equals(other.secondaryData))
            return false;
        return true;
    }

    public static void main(String[] args)
    {
        IntHolder ih1 = new IntHolder(1, 5);
        IntHolder ih2 = new IntHolder(3, 3);

        System.out.println("hashCodes: ih1: " + ih1.hashCode() + ", ih2: " + ih2.hashCode());
        System.out.println("equals: " + ih1.equals(ih2));
    }
}

For reference, Eclipse's auto-generated hashCode() for the IntHolder class is:

@Override
public int hashCode()
{
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((primaryData == null) ? 0 : primaryData.hashCode());
    result = prime * result
            + ((secondaryData == null) ? 0 : secondaryData.hashCode());
    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

1
String str1="abcdef";
String str2="abcdfG";

They both have the same hashcode and equals method returns false.

Comments

0
public class Employee {

protected long   employeeId;

public boolean equals(Object o){
if(o == null)                return false;
if(!(o instanceof) Employee) return false;

Employee other = (Employee) o;
return this.employeeId == other.employeeId;

}

public int hashCode(){
return (int) this.employeeId;

}

}

In this example, we have overridden the equals method - two employees are equal when they will have same employee id.

If two Employee objects are equal, they will also have the same hash code.

Your Ans -

In this example, we also implemented the hash code - hashcode is the employeeId that is rounded down to an int. That means that many employee id's could result in the same hash code, but these Employee objects would still not be equal, since they don't have the same employee id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.