Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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

Problem faced in using hashset(comparison of equal elements)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chhabra rahul wrote:




modify the code as followes

(this.name).equals(((dog)o).name)
 
Sheriff
Posts: 22647
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22647
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic