• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hash Set Question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I was going through a book when i just came accross a code that i thought should return a boolean value true but is returning false. Am i doing something wrong here?

Below is the code i am trying


The result it is returning is false. I thought it should be true.

Thanks
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Define a sensible hash code method. If two objects equal (in the equals sense) they should share the same hash code.
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If equals() is overridden, so should be hashCode.

$ java main
19972507
32942009
8222510
18581223
3526198
7699183

 
manjo franko
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks or the reply. I will implement a hash code. If i remove the equals method would it work? I still get a false. I would like to why is it so..
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

manjo franko wrote:Thanks or the reply. I will implement a hash code. If i remove the equals method would it work? I still get a false. I would like to why is it so..




If you don't override both equals() and hashCode(), then it will inherit both those methods from the Object class. The Object class is designed to say that two instances are equal only if the references are pointing to the exact same instance. In other words, if the "==" comparison say that they are equal. Does that work? Well, is that what your application wants?


And by the way, the term "still get a false" is incorrect. With inheriting both equals and hashcode from the Object class, you will consistently get false. With your original program, the contract is broken, and you will get false most of the time. It is theoretically possible to get true -- although not very likely.

Henry

 
manjo franko
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks For the reply.

With inheriting both equals and hashcode from the Object class, you will consistently get false.



Did you mean with/Without?. The question i had was that without overriding the equals or the hashcode when i store the values in a Hash Set and do a contains i am assuming that it might have the equals and hashcode implemented. Would this be correct then- when i am using the hash set to store my objects the right way to use the contains is that i implement a hashcode and a equals method in object that i am going to do the contains on?

Thanks,
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The general contract is that if two objects are considered equal, their hash code should be equal too. If you violate this, hash-code base data structures won't work with objects of this type. When an element is sought for in a hashset, it is compared against the objects in the same bucket (derived form the hash code value). In your example, different Points created with the same coordinates are considered equal, however their hash codes differ. It is clear a hash code based data structure can not work with them.
 
manjo franko
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answers guys! I got it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic