aspose file tools*
The moose likes Java in General and the fly likes Problem faced in using hashset(comparison of equal elements) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem faced in using hashset(comparison of equal elements)" Watch "Problem faced in using hashset(comparison of equal elements)" New topic
Author

Problem faced in using hashset(comparison of equal elements)

chhabra rahul
Greenhorn

Joined: Jul 18, 2009
Posts: 2
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

Joined: Aug 26, 2008
Posts: 263
chhabra rahul wrote:




modify the code as followes

(this.name).equals(((dog)o).name)

SCJP 1.5(97%) My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

You haven't overridden hashCode(), but created a new method that has almost the same name.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
chhabra rahul
Greenhorn

Joined: Jul 18, 2009
Posts: 2
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

The name of your hashcode method is still wrong -- it must be hashCode(), with a capital 'C'.


[Jess in Action][AskingGoodQuestions]
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
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

Joined: Jun 20, 2009
Posts: 32
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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

Joined: Jul 14, 2009
Posts: 11
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

Joined: Jun 20, 2009
Posts: 32
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 :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem faced in using hashset(comparison of equal elements)