• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Dough in HashSet ::

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Police line, do not cross. Well, this tiny ad can go through:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic