J
Javas
import java.util.*;
public class Qn353
{
private String name;
public Qn353(String name)
{
this.name = name;
}
public boolean equals(Object o)
{
if ( ! (o instanceof Qn353) ) return false;
Qn353 p = (Qn353) o;
return p.name.equals(this.name);
}
public static void main(String [] args)
{
HashSet<Object> hs = new HashSet<Object>();
hs.add(p1);
hs.add(p2);
System.out.println(hs.size());
}
public int hashcode()
{
return 1;
}
}
Above code outputs : 2
But I thought it is supossed to be 1 since the hashcode is the same
for both the objects, equals also holds true and the set doesnt allow
duplicates. What is wrong? Please correct me.
Thanks!
public class Qn353
{
private String name;
public Qn353(String name)
{
this.name = name;
}
public boolean equals(Object o)
{
if ( ! (o instanceof Qn353) ) return false;
Qn353 p = (Qn353) o;
return p.name.equals(this.name);
}
public static void main(String [] args)
{
HashSet<Object> hs = new HashSet<Object>();
hs.add(p1);
hs.add(p2);
System.out.println(hs.size());
}
public int hashcode()
{
return 1;
}
}
Above code outputs : 2
But I thought it is supossed to be 1 since the hashcode is the same
for both the objects, equals also holds true and the set doesnt allow
duplicates. What is wrong? Please correct me.
Thanks!