• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

my hashCode() method returns negative

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show the code of both equals() and hashCode()?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic