• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hash Set Question  RSS feed

 
manjo franko
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • 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: 999
5
  • Mark post as helpful
  • send pies
  • 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
  • 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..
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 999
5
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for the answers guys! I got it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!