There are two methods that a class needs to override to make objects of that class work as hash map keys.
public int hashCode(); public boolean equals(Object o);
But you must obey the contract between hashCode and equals method while overriding them in your new Class.
Contract for hashCode and equals method
The equals() method must return true if the fields of the current object equal those of the object passed in, else return false. By “equal”, we generally mean that primitive fields match via the == operator, and objects are either both null or both non-null and match via the equals() method. Note two important constraints on equals():
if x.equals(y) returns true, then the hash codes of x and y must be identical;
- it must be reflexive and transitive: that is, x.equals(y) must return the same value as y.equals(x), and if x.equals(y) and y.equals(z), then x.equals(z) must also be true (see below for what this actually means in real terms!).