[Logo]
Forums Register Login
Problem faced in using hashset(comparison of equal elements)
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
 
 
chhabra rahul wrote:




modify the code as followes

(this.name).equals(((dog)o).name)
You haven't overridden hashCode(), but created a new method that has almost the same name.
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
 
The name of your hashcode method is still wrong -- it must be hashCode(), with a capital 'C'.
change

public int hashcode() to public int hashCode ()

actually as Rob mentioned you are not overriding the hasCode() you are creating your own method.
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
 
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.
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.
 
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 :-)
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Dec 13, 2017 19:41:48.