Forums Register Login
Overriding hashCode()
Hi Ranchers,

I am overriding the equals() method of the object.
Class Blah
public abstract String getOid();
public boolean equals(Object o)
return this.getOid().equals(((Blah)o).getOid());
I assume If the oid's of the two classes are equal then the two objects are equal.
I read that its a best practice to override hashCode() method when you override equals() method and it should return the same hashCode if two objects are equal.
What should be the implementation for the hashCode() method?

Thanks in advance
If oid is a String, you can return its hashCode.
How does it Guarantee the hashCode() method returns same hashCode for two objects?
If the object equals() doesnt simply depend on oid and is more complex then how to implement it?
An efficient way would be to return the same hashCode every time.

But to make it more efficient, you would need to, depending on what makes the two objects equal, make sure the hashCodes are the same if the objects are equal.
Anderson gave himself the promotion. So I gave myself this tiny ad:
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 1171 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 15, 2018 23:09:58.