I'm having an issue where the item that I'm trying to remove is in my HashSet, but both contains()
and remove()
return false. However, if I write a simple for loop:
HashSet<Node> myList;
myList.add(new Node("node"));
Node myNode = new Node("node");
for (Node n : myList){
if (n.equals(myNode)) System.out.println("Found Node in List");
}
I get the output as expected. If I switch for myNode.equals(n)
, it works (equals is symmetrical).
I use my equals method (written for Node) successfully elsewhere, so I know that it works.
Does HashSet use the pointer, or possibly some other parameter of the object to compare? Despite not wanting duplicates, would I be better off using an arrayList and manually checking that I'm not adding a duplicate?