I have the following code:
public class MyElement {
String name;
String type;
MyElement(String name, String type) {
this.name = name;
You need to implement equals(Object o)
and hashCode()
on MyElement per the general contract. Absent that Set.contains()
will use the default implementation which compares the memory address of the objects. Since you're creating a new instance of MyElement in the contains call it comes back as false.
You should override an equals(MyElement me) function. Equals returns a boolean
Otherwise, you are checking that two items are the same instance of an object, not that their internal content is the same.
MyElement(String name, String type) {
this.name = name;
this.type = type;
}
public boolean Equals<MyElement>(MyElement me) {
return this.name.equals(me.name) && this.type.equals(me.type);
}