• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem faced in using hashset(comparison of equal elements)

 
chhabra rahul
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this code to check function of hashset. equals() method is overridden in dog class. I am trying to add two dogs with same name which should not be allowed.
Can anybody tell me what is the problem.



Output is



browniebrownie
equals test true
true true true false false true
brownie
java.lang.Object@42e816
brownie
45
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chhabra rahul wrote:




modify the code as followes

(this.name).equals(((dog)o).name)
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't overridden hashCode(), but created a new method that has almost the same name.
 
chhabra rahul
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made the suggested changes but new dog object d2 can still be added which should not happen



browniebrownie
equals test true
true true true false false true
brownie
java.lang.Object@42e816
brownie
45
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name of your hashcode method is still wrong -- it must be hashCode(), with a capital 'C'.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change

public int hashcode() to public int hashCode ()

actually as Rob mentioned you are not overriding the hasCode() you are creating your own method.
 
James Dixon
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chhabra
One other thing about the hashCode method you show in the second listing. In it you are derriving the code from the length of the variable 'name'. If it's just the name that you are concerned about then it would be better for you to at least use name.hashCode().

The reason for this is that when you add a new record to something like a HashSet it first carries out a quick search through the set to see if the object has previously been added. It does this by using the hashCode method first, and then if it finds an object with the same hashCode it follows up by running through the equals method to make sure that they are the same... This means that if all your objects have the same hashCode it will also have to go through both hashCode and equals each time you add to the set, which as you can imagine won't be that efficient.

for more information about this have a look at the following article:
http://www.ibm.com/developerworks/java/library/j-jtp05273.html
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Dixon wrote:One other thing about the hashCode method you show in the second listing. In it you are derriving the code from the length of the variable 'name'. If it's just the name that you are concerned about then it would be better for you to at least use name.hashCode().

Although you are right that name.hashCode() would be better, name.length() is still a valid return value. After all, if two dogs have the same name, the length of their names is also the same.
 
Daniel Näslund
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the @Override notation. It saves time and prevents headaches!

There are many legal hashCodes. If you are looking for good performance than as few values as possible should cause collisions (end up with the same hash value.
 
James Dixon
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Although you are right that name.hashCode() would be better, name.length() is still a valid return value. After all, if two dogs have the same name, the length of their names is also the same.

True using the length of the name would work fine, but as the HashSet allows other object types to be stored, it is still potentially more efficient to make the hashCode return something that would be unique to the Dog class.

To be honest in the example code above it wouldn't make much difference, but I thought it was at least worth pointing out to Chhabra as it may prove useful in the future :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic