contains method ignores equals override
equals, java, overriding, set
Solution
public boolean equals(Node cmpCell)
This is not a valid override. The syntax of `equals` method of `Object` class is: -
public boolean equals(Object)
And yes, as pointed out by @JonSkeet in comment, whenever you are overriding `equals` method, also remember to override `hashCode` method to follow the contract of `equals` and `hashCode`. Because if you don't do that, then even if your `equals` method shows evaluates your instances as equal, the default `hashCode` implementation in `Object` class will generate `different hashCodes` for them, and hence they won't be equal.
Also, ensure that, while calculating `hashcode`, you consider only those attributes, that you used to compare your instances in `equals` method. Else, again you will get incorrect result.
In addition to that, if you are using any IDE like Eclipse, it generates a very nicely overridden and compatible `equals` and `hashCode` method for you. You should be better using them. You need to `right-click` on your class, go to `source` and select `Generate equals and hashCode method`.
Problem
Having `Node` abstract class which `Cell` extends him . In `Cell` I implemented `public boolean equals(Node cmpCell)` . I `created Set<Node> closeList = new HashSet<Node>();` and when I execute `closeList.contains((Cell) node)` I debugged it and detect that it utterly ignores `Cell equals` I implemented . What I did wrong ? Edit : I changed in `Cell` to ``` @Override public boolean equals(Object cmpCell) ``` and still `closeList.contains((Cell) node)` doesn't using the above override . 2nd Edit : In `Cell` class there is 2 members - ``` int colIndex ; int rowIndex ; ``` the `equals` override just compare them to that both members of the 2nd class , I think it would be better I use `HashMap<K, V>` but still I would be glade to know how the `hashCode` should be looks like in such case ?