• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dough in HashSet ::

 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i believe to avoid entry of duplicate objects you not only have to override equals method but also hashcode() method which you didnot..
 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic