• Post Reply Bookmark Topic Watch Topic
  • New Topic

my hashCode() method returns negative  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object Foo whose hashCode() returns a negative integer. Is there anything wrong with that ?
When I have a set of Foo instances, I am not able to delete a particular instance from the set.

Why can't I remove the object toBeDeleted since its hashCode is equal to one of the objects in the set s ?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Negative hashCodes are fine.
Given what you've shown below, I'd say that the Foo class didn't actually override both hashCode() and equals(), or you didn't do it properly. You've got to override both for this to work correctly, such that f1.equals(toBeDeleted) returns true, and f1.hashCode() == toBeDeleted.hashCode() .
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, thanks for your reply.
Here's another conundrum:

Notice the last line. Why is it behaving like this ?
Just to give you a bit more info; the code above only fails when it's within a domain object using Hibernate framework. But it works fine from the command line ? I am at a loss ?
Regards,
Pho
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest's previous response applies equally well here. If you override hashCode() wothout correctly overriding equals() as well - or vice versa - then the methods of HashMap, Hashtable, and HashSet will not function as expected. Details of how to override hashCode() and equals() can be found in most introductoray Java texts - the Java Tutorial is one example.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
My equals/hashCode() works fine in my Junit test cases.
I implemented them following:
http://www.geocities.com/technofundo/tech/java/equalhash.html
But it fails to work when deployed to the container.
Regards,
Pho
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the code of both equals() and hashCode()?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one theory: you made the common mistake of implementing equals() like this:
public boolean equals(Foo arg) ...
using the type of the class as the type of the method parameter. This overridden equals won't be used by the container; the container will continue to use the default version of equals.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code for hashCode/equals is as follows. EqualsBuilder and HashcodeBuilder is from the Apache commons project whose javadoc is available here

[ March 10, 2004: Message edited by: Pho Tek ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!