Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to override equals and hashcode methods? [duplicate]

Tags:

java

equals

Possible Duplicate:
Overriding equals and hashCode in Java

If I have

class A {
    int x = 1;
}
...
A a1 = new A();
A a2 = new A();
a1.equals(a2);

If I compare 2 instances of A without override the equals method, will I get expected result?

like image 635
user926958 Avatar asked Oct 30 '12 06:10

user926958


People also ask

When should we override hashCode and equals?

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.

Is it mandatory to override hashCode If you override equals method?

If you override the equals(), you MUST also override hashCode(). Otherwise, a violation of the general contract for Object. hashCode() will occur, which results in unexpected behavior when your class is in conjunction with all hash-based collections.

What will happen if we don't override hashCode and equals method?

If you don't override hashcode() then the default implementation in Object class will be used by collections. This implementation gives different values for different objects, even if they are equal according to the equals() method.

What happens if hashCode () method always return same value?

If multiple objects return the same value from hashCode(), it means that they would be stored in the same bucket. If many objects are stored in the same bucket it means that on average it requires more comparison operations to look up a given object.


2 Answers

If I compare 2 instances of A without override the equals method, will I get expected result?

That depends on what you expect :)

The default implementation will give you reference equality - in other words, when you compare two references, equals will only return true if they're references to the same object.

You would normally override equals to implement "value equality" - where two distinct objects are deemed equal, usually by virtue of having equal field values themselves. The exact meaning of equality will depend on your design - the two objects could still be distinguishable in other ways, for example.

If you override equals, you should also override hashCode to be consistent with equals, such that if a.equals(b) is true, then a.hashCode() == b.hashCode(). This will allow instances of your class to be used as keys in hash-based collections (e.g. HashMap) so that you can look up a value based on a key which is equal to the original one, rather than having to use a reference to the exact original key object.

like image 173
Jon Skeet Avatar answered Oct 23 '22 09:10

Jon Skeet


If I compare 2 instances of A without override the equals method, will I get expected result?

No. Since you have created two different instances explicitly.

Why? The default implementation of equals checks whether the two concerned objects point to the same memory location in java virtual memory (and this default behavior is defined in java.lang.Object.equals())

When do I need to override equals and hashcode methods?

The most common scenario when programmers override both equals() and hashcode() are if you need to use instances of the concerned class as

  1. Keys in java.util.Map implementations
  2. Values in java.util.Set implementations
  3. You want to check equality of values between two different instances of the same class (overriding equals() is mandatory in this case, hashcode() is not - but is a good programming practice to)

General contract of equals and hashcode is :

if a1.equals(a2)
    it is mandatory that a1.hashcode() == a2.hashcode()
if a1.hashcode() == a2.hashcode()
    it is not mandatory that a1.equals(a2)

I guess that enough data to process for a day :)

like image 32
Amit Sharma Avatar answered Oct 23 '22 08:10

Amit Sharma