Why is the output of this program false
? I am expecting true
as n object initialize with same string what I am checking for.
public class Test {
public static void main(String[] args) {
Name n = new Name("jyoti", "meher");
Set<Name> s = new HashSet();
s.add(n);
System.out.println(s.contains(new Name("jyoti", "meher")));
}
}
class Name {
String name, title;
public Name(String name, String title) {
this.name = name;
this.title = title;
}
public boolean equals(Object o) {
if (!(o instanceof Name)) {
return false;
}
Name n = (Name) o;
return n.name.equals(name) && n.title.equals(title);
}
}