I have the following code:
public class MyElement { String name; String type; MyElement(String name, String type) { this.name = name; this.type = type; } } public class Test { public static void main(String[] args) { Set<MyElement> set = new HashSet<MyElement>(); set.add(new MyElement("foo", "bar")); set.add(new MyElement("foo", "bar")); set.add(new MyElement("foo", "bar")); System.out.println(set.size()); System.out.println(set.contains(new MyElement("foo", "bar"))); } }
which, when executed, returns with:
3 false
I expected the result to be 1 and true. Why are my elements not recognized the same and how can I fix this? Thanks, Wayne.
Wayne source share