If you go with the object solution, make sure your key object is immutable.
Otherwise, if somebody mutates the value, not only will it no longer be equal to other apparently-identical values, but the hashcode stored in the map will no longer match the one returned by the hashCode()
method. At that point you're basically SOL.
For instance, using java.awt.Point
-- which looks, on paper, like exactly what you want -- the following:
public static void main(String[] args) {
Map<Point, Object> map = new HashMap<Point, Object>();
Point key = new Point(1, 3);
Object val = new Object();
map.put(key, val);
System.out.println(map.containsKey(key));
System.out.println(map.containsKey(new Point(1, 3)));
// equivalent to setLeft() / setRight() in ZZCoder's solution,
// or setX() / setY() in SingleShot's
key.setLocation(2, 4);
System.out.println(map.containsKey(key));
System.out.println(map.containsKey(new Point(2, 4)));
System.out.println(map.containsKey(new Point(1, 3)));
}
prints:
true
true
false
false
false