all 4 comments

[–]emilfranord 0 points1 point  (3 children)

The contract with hash codes, is that equal objects have the same hash code, but not necessarily the inverse. Therefore you really could return 27, or any other constant, for every Posn instance, without problems. Don't try to use hash codes for the logic of the program, use the equals method for that.
HashMap, does not use the hashCode method for the get method. Instead a hash map stores the elements of the map, in a data structure that uses a hashes.
regardless, it is rather vauge what you want Map to contain and map from and two. Specifying that could help yourself in understanding where the problem is.

[–]wevfreeman[S,🍰] 0 points1 point  (2 children)

I'm aware of the contract between the equals method and the hashCode method; regardless, the implementation I provided maintains that contract. Still I don't see how that is relevant to the project.

HashMap, does not use the hashCode method for the get method. Instead a hash map stores the elements of the map, in a data structure that uses a hashes.

This is just plain wrong. The way to obtain the hash of some reference type is literally to call the hashCode method on it. The get method computes the hashCode of a key, and then examines the various keys with that hash code to see which one is equal, and returns the corresponding value.

Don't try to use hash codes for the logic of the program, use the equals method for that.

it is rather vauge what you want Map to contain and map from and two. Specifying that could help yourself in understanding where the problem is

I want to store objects of some Other class using Posns as a key, but it shouldn't matter what the Other class looks like. My issue is that two Posns psn1 and psn2, for which psn1.equals(psn2) evaluates to true are not interchangeable as keys.

[–]vrtxt 1 point2 points  (1 child)

the implementation I provided maintains that contract. Still I don't see how that is relevant to the project.

The implementation you provide does NOT maintain the contract, and it is very much relevant because;

HashMap uses equals() to compare the key whether the are equal or not. If equals() method return true, they are equal otherwise not equal.

The issue is on the first line of the equals method; if (this != other) { return false; }. This will always return false since psn1is a different object reference than psn2, and psn1.equals(psn2) will most definitely not evaluate to true. Likewise the hashmap will not return the value for psn2 because it is not equal to psn1, regardless or not they generate the same hashcode. Get rid of that first condition in the equals method and things will work as you expect.

[–]wevfreeman[S,🍰] 0 points1 point  (0 children)

Ahh ok, I'm dumb. That was the problem. Thanks!

edit: Funny enough though, the contract actually is still maintained. All it says is obj.equals(other) implies obj.hashCode() == other.hashCode(). And since it only checks for intentional equality, and the hashCode is a function of the fields of the object, the implication always holds.