0

Possible Duplicate:
Overriding equals and hashCode in Java

All,

I have defined my class and which to override equals() and hashCode () methods as well. But I have no idea of what specifically these methods should be implemented for my own class. Can anyone please shed some light on the implementation of these methods for custom classes?

Community
  • 1
  • 1
name_masked
  • 9,544
  • 41
  • 118
  • 172
  • That actually depends on how you define equality. Often all or nearly all attributes need to be considered, sometimes only one / a few, and sometimes (think of Threads for example), all you want in your equals() method is to compare identity. – helpermethod Oct 12 '10 at 22:17

6 Answers6

9

You say you already know which one of your custom classes need to override hashCode/equals? Then you also know what attributes (global variables) determine equality of each class.

When you know these attributes you can implement hashCode/equals either manually or by generating the methods using a modern IDE such as Eclipse, NetBeans, etc. In Eclipse there's an option named "Generate hashCode() and equals()" under the "Source" menu

alt text

Richard Kettelerij
  • 2,049
  • 14
  • 17
3

For equals, the answer depends on what your business need is, i.e. what does it mean for your objects to be equals.

hashCode() should always return a unique value for an object, unless that object is equal to another object. It should depend on the values of the properties on your object.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
2

Java theory and practice: Hashing it out

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • This answer is a link to a page on IBM that is returning back a 503 page. Do you think you could update your answer to include the relevant information instead of being just a link? –  Aug 11 '14 at 11:19
  • @MichaelT This link is still active.... – Aaron McIver Aug 13 '14 at 04:44
2

Basically, if you want to store an object in a collection (Map, Set, List) then you have to implement equals and hashCode methods according to the contract defined in the documentation.

Otherwise, many collection implementations won't have the expected behaviour. For implementation clues, read the Object Javadoc for equals and hashcode.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • Only *keys* in a hash must define the hash code correctly. I can store my Froboz objects in a HashMap with no override of the `equals()` or `hashCode()` methods if I'm using, say, a `String` as the key, and with String, the `equals()` and `hashCode()` implementation are already correctly defined. – jbindel Oct 12 '10 at 22:09
  • True. But http://download.oracle.com/javase/6/docs/api/java/util/Collection.html#contains(java.lang.Object) will fail. – Brian Clozel Oct 12 '10 at 22:14
1

Read the API documentation of the two methods in java.lang.Object. It describes very exactly how overriding implementations should behave.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1

When implementing equals() make sure you understand the difference between equality and identity. Two object instances may be 'equal' but may not be identical. a.equals(b) is a test for equality, which your business rules should define. == is a test for object identity (same object instance)

Joe
  • 3,337
  • 1
  • 14
  • 11