A Set
of Coordinate instances
No. 191
Inserting Coordinate
instances into a Set
.
Q: |
Create a Did you expect this behaviour? Explain this result and a solution. |
A: |
Our code is very similar to Inserting strings into a
Since we do have set semantics we expect the duplicate
coordinate value The set contains 3 elements: (4|1) (1|2) (1|2) This is due to our own fault not providing a
This yields the following output: c12.hashCode() and c12Duplicate.hashCode():1334574952,1882008996 Apparently the two instances c12 and c12Duplicate are
equal to each other. Their hash codes however are different
clearly violating the contract being described in Java
Collections - hashCode() and equals(). The values
actually stem from The former exercise Inserting strings into a
This yields: c12.hashCode() and c12Duplicate.hashCode():33,33 And finally: The set contains 2 elements: (1|2) (4|1) |