Say I have 2 lists of Nodes (Node is some comparable object, the details don't matter). I'd like to find which Nodes appear on both lists - not necessarily the same Node object, but Nodes whose compareTo returns 0.
For example, nodeA is on list A and nodeB is on list B. They are NOT the same Node but nodeA.compareTo(nodeB) will return 0 (equality).
I'd like to solve this by iterating over list A and putting the nodes in a HashMap/HashSet, then iterating over list B and checking which nodes have already been inserted into the HashMap/HashSet. The issue is, this will not work as the Nodes will not be considered equal by the HashMap unless they are the same actual Node.
How can I solve this problem? Note that I am not looking for other solutions to this problem, but for an understanding of if (and how) I can use a HashMap, HashSet or another similar data structure in the given example scenario.
nodeA.hashcode() == nodeB.hashcode()returns?compareTouse aTreeSet. For HashMap you needequals/hashCodeto be written correctly.