This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Self Test Pg 617, Q12- HashSet doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Self Test Pg 617, Q12- HashSet doubt" Watch "K&B Self Test Pg 617, Q12- HashSet doubt" New topic
Author

K&B Self Test Pg 617, Q12- HashSet doubt

Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
When i run this program I see that all five Person Objects are added to HashSet. HashSet doesnot add duplicates. Then why is "Hans" and "Jane" added twice to HashSet??

public static void main(String[] args) {
Group g = new Group();
g.add(new Person("Hans"));
g.add(new Person("Lotte"));
g.add(new Person("Jane"));
g.add(new Person("Hans"));
g.add(new Person("Jane"));
System.out.println("Total: " +g.size());
}}
public boolean add(Person o)
{
System.out.println("Adding: "+ o);
return super.add(o);
}

}
class Person{
private final String name;
public Person(String name){ this.name = name;}
public String toString(){ return name;}

}

Output:
Adding: Hans
Adding: Lotte
Adding: Jane
Adding: Hans
Adding: Jane
Total: 5


Thank you
sirisha


If you worry you cannot work... If you work you need not worry
sharad sinha
Greenhorn

Joined: Jul 13, 2008
Posts: 26

When i run this program I see that all five Person Objects are added to HashSet. HashSet doesnot add duplicates. Then why is "Hans" and "Jane" added twice to HashSet??


What makes you to think that the two Person Objects should be same only because they have similar names ???
Did you do anything in your Person class to tell the JVM how to test for equality?


Thanks, Sharad<br />SCJP 5 (July, 2008)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
You must implement (override) hashCode() method in PersonClass.
HashSet uses hashcodes to store/search objects. hashCode must return the same value for identical objects. In your Person class hashCode() from Object is used, which by default returns different values for each individual object, and therefore HashSet treats both "jane" persons as different.
Try simple hashCode below , it should work:

[ August 26, 2008: Message edited by: Ireneusz Kordal ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

"Sirishaaaaa Ghatty",
did you check Bear's message from this thread ?


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Self Test Pg 617, Q12- HashSet doubt
 
Similar Threads
Generics K&B doubt
what is the correct answer?
Another Generics question
HashSet
A Generics and Collections Problem