The equals() documentations states (emphasis added):
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
public boolean add(E e)
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.
Specified by:
add in interface Collection<E>
Specified by:
add in interface Set<E>
Overrides:
add in class AbstractCollection<E>
Parameters:
e  element to be added to this set
Returns:
true if this set did not already contain the specified element
I hadn't been here for fifteen years
Aahan Agrawal wrote:
Also, how should I construct a hashCode() method such that every unique coordinate returns a unique integer? Is there some mathematical function that, using both x and y, could return a distinct output for every distinct combination of x and y?
Some function of the form x^n + y^m comes to mind, but I'm not sure how to determine if this would map a unique int to every x and y?
Paweł Baczyński wrote:Also note that your equals() method violates the contract of Object.equals()...
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Unfortunately they do not give enough information to find that paper, so one cannot verify that assertion at the source. Angelika Langer is a bit more optimistic about equals() methods and lists some correct and incorrect implementations; you may need to follow that link to next section several times to find that piece.Winston Gutkowski wrote: . . . this excellent article[/url] warns us that:
"The authors of a 2007 paper concluded that almost all implementations of equals() are faulty."
. . .
She'll be back. I'm just gonna wait here. With this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardenergift.com
