/** * Point represents an (x,y) coordinate pair * This class exists solely to show an example of overriding hashCode(). * Remember, you only need to override hashCode() if you override equals(). */ public class Point { private int x, y; @Override public int hashCode() { // We want two Points that are equal to return the same hash code. // Because two Points with the same (x,y) values are equal, we make // the hash code a function of x and y. Because every Java object has // the hashCode() method, our hash code can be a function of the hash // codes of the instance fields. return ((Integer)x).hashCode() * ((Integer)y).hashCode(); } @Override public boolean equals(Object o) { if (o instanceof Point) { Point p = (Point) o; return x == p.x && y == p.y; } return false; } }