I have 2 Hash Sets, both containing x amount of "Names" (Object). What I would like to do is find out if my "Names" in Names1 or in Names2 ?
public static void main (String[] args) {
Set<Name> Names1 = new HashSet<Name>();
Names1.add(new Name("Jon"));
Names1.add(new Name("Mark"));
Names1.add(new Name("Mike"));
Names1.add(new Name("Helen"));
Set<Name> Names2 = new HashSet<Name>();
Names2.add(new Name("Mark"));
Names2.add(new Name("Mike"));
Names2.add(new Name("Sally"));
Set<Name> listCommon = new HashSet<Name>();
for (Name element : Names1) {
if (!Names2.contains(element)) {
listCommon.add(element);
}
}
for (Name element : listCommon) {
System.out.println(element.getNameString());
}
}
public class Name {
String ord;
Name(String ord1){
ord = ord1;
}
public String getNameString(){
return ord;
}
}
So when I ran this code, I got no output at all, cause the
'if (!Names2.contains(element)) {'
never occurred. But what I would like to get as output would be Jon and Helen. Since they are not in Names2.