my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dough in HashSet :: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dough in HashSet ::" Watch "Dough in HashSet ::" New topic
Author

Dough in HashSet ::

Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
HashSet does not allow duplicate object. In the following
code how it is allow two same objects?
Is there any one to spot my error ?

/* b.add( g.add(new Person("Hans")));
b.add( g.add(new Person("Lotte")));
b.add( g.add(new Person("Jane")));
b.add( g.add(new Person("Hans")));
*/



import java.util.*;
public class Group extends HashSet<Person> {
public static void main(String[] args) {
Group g = new Group () ;
List<Boolean> b = new ArrayList<Boolean>();
b.add( g.add(new Person("Hans")));
b.add( g.add(new Person("Lotte")));

b.add( g.add(new Person("Jane")));
b.add( g.add(new Person("Hans")));
b.add(g.add(new Person("Jane")));
System.out.println ("Total: " + g.size() );
Iterator c =b.iterator();
while(c.hasNext())
{
System.out.println (c.next());

}
}
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; }
public boolean equals (Person p)
{
boolean test = false;
if( (p !=null) &&( p instanceof Person))
{
if (p.name ==this.name)
{
test= true;
}
}
return test;

}
}

output :::

Adding: Hans
Adding: Lotte
Adding: Jane
Adding: Hans
Adding: Jane
Total: 5
true
true
true
true
true


SCBCD, SCJP & MCP
HowToAskQuestions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

There is a bug in your code, in class Person. This line is wrong:

if (p.name ==this.name)

It should be:

if (p.name.equals(this.name))

You should never compare strings using "=="; use "equals(...)" instead.

(Please use code tags when you post source code, that will make the code look better on JavaRanch).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
i believe to avoid entry of duplicate objects you not only have to override equals method but also hashcode() method which you didnot..


"History would be kind to me, for I intend to write it."
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
Originally posted by Sabanayakam Thillakan:
HashSet does not allow duplicate object. In the following
code how it is allow two same objects?
Is there any one to spot my error ;) ?

/* b.add( g.add(new Person("Hans")));
b.add( g.add(new Person("Lotte")));
b.add( g.add(new Person("Jane")));
b.add( g.add(new Person("Hans")));
*/



import java.util.*;
public class Group extends HashSet<Person> {
public static void main(String[] args) {
Group g = new Group () ;
List<Boolean> b = new ArrayList<Boolean>();
b.add( g.add(new Person("Hans")));
b.add( g.add(new Person("Lotte")));

b.add( g.add(new Person("Jane")));
b.add( g.add(new Person("Hans")));
b.add(g.add(new Person("Jane")));
System.out.println ("Total: " + g.size() );
Iterator c =b.iterator();
while(c.hasNext())
{
System.out.println (c.next());

}
}
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; }
public boolean equals (Person p)
{
boolean test = false;
if( (p !=null) &&( p instanceof Person))
{
if (p.name ==this.name)
{
test= true;
}
}
return test;

}
}

output :::

Adding: Hans
Adding: Lotte
Adding: Jane
Adding: Hans
Adding: Jane
Total: 5
true
true
true
true
true
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Jesper Young:
There is a bug in your code, in class Person. This line is wrong:

if (p.name ==this.name)

It should be:

if (p.name.equals(this.name))

You should never compare strings using "=="; use "equals(...)" instead.

(Please use code tags when you post source code, that will make the code look better on JavaRanch).


Generally speaking, that's right. However, in this case that's not the problem because all the strings he uses when instantiating the Persons are interned (the code is using string literals).

As Priyam said, the problem resides in the fact he did not override hashCode().


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
Hi Guys
Sorry for the code block.
i have figure out the problem. if you use the following code block
output will be

Adding: Hans
Adding: Hans
Total: 1
true
false


[CODE BLOCK]

Group g = new Group () ;
List<Boolean> b = new ArrayList<Boolean>();
Person t = new Person("Hans");
b.add(g.add(t));
b.add(g.add(t));
System.out.println ("Total: " + g.size() );
Iterator c =b.iterator();
while(c.hasNext())
{
System.out.println (c.next());
}

[CODE BLOCK]

thank you guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dough in HashSet ::
 
Similar Threads
chapter 7: K&B
HashSet
K&B Chapter 7 Question # 12
Generics K&B doubt