I have a custom class called Position. I want to use the following:-
Set<Position> s=new HashSet<Position>();
Do I have to override the HashCode() method in the Position class? I have not overridden equals() method. I want two Position objects to be considered equal only when they are the same object. Do I still have to override HashCode() in order to use the HashSet as shown above?
You only have to override
hashcodeandequalsif you want different objects (presumably representing the same thing) to count as equal.(To clarify, if you do override one you should override the other, so that objects that test as equal always have the same hashcode.)