単純なクラスがあるとします:
public class Point implements Comparable<Point> {
public int compareTo(Point p) {
if ((p.x == this.x) && (p.y == this.y)) {
return 0;
} else if (((p.x == this.x) && (p.y > this.y)) || p.x > this.x) {
return 1;
} else {
return -1;
}
}
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
HashMap
からPoint
何かへ、たとえば:Cell
次に
cellMap = new HashMap<Point, Cell>();
、cellMap
次のように入力します。
for (int x = -width; x <= width; x++) {
for (int y = -height; y <= height; y++) {
final Point pt = new Point(x,y);
cellMap.put(pt, new Cell());
}
}
}
そして、(些細な)次のようなことをします:
for (Point pt : cellMap.keySet()) {
System.out.println(cellMap.containsKey(pt));
Point p = new Point(pt.getX(), pt.getY());
System.out.println(cellMap.containsKey(p));
}
そして、1 番目と 2 番目のケースで、それぞれtrue
とを取得します。false
何が起こっている?このマップは値ではなくハッシュを比較していますか? 両方のケースで例が true を返すようにする方法は?