当我创建对象的新实例作为键时,Java HashMap.get()返回null

我正在制作一个电子表格应用程序,我正在使用HashMap将数据存储在单元格中。 作为关键我使用的是Point类,它只有行数和列数。 我遇到的问题是,如果我使用带有新Point的HashMap.get(),它将返回一个空值。

HashMap cache = new HashMap(); Point p1 = new Point(1,1); Point p2 = new Point(2,2); cache.put(p1, "Test: 1,1"); cache.put(p2, "Test: 2,2"); int maxRow = 2; int maxCol = 2; for (int i = 1; i <= maxRow; i++) { for (int j = 1; j <= maxCol; j++) { System.out.print(cache.get(new Point(i,j))); if (j < maxCol) System.out.print("\t"); if (j == maxRow) System.out.println(""); } } 

返回:

 null null null null 

我可能错过了一些明显的东西,但我自己找不到。 此外,如果您碰巧知道是否有更好的数据结构来存储来自单元格的数据,我很乐意听到它。 提前致谢!

为了详细说明我的上述评论,你的Point类应该实现hashcode并且等于如下:(存在许多实现,它只是一个有效)假设你的实例的变量是xy

  public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; if (x != point.x) return false; if (y != point.y) return false; return true; } public int hashCode() { int result = x; result = 31 * result + y; return result; } 

否则,如果你不覆盖这些方法, Object的Javadoc很好地解释了你的问题:

 As much as is reasonably practical, the hashCode method defined by * class {@code Object} does return distinct integers for distinct * objects. (This is typically implemented by converting the internal * address of the object into an integer, but this implementation * technique is not required by the * JavaTM programming language.) * * @return a hash code value for this object. * @see java.lang.Object#equals(java.lang.Object) * @see java.lang.System#identityHashCode */ public native int hashCode(); 

因此, new Point(1,2)不会被视为等于new Point(1,2) ,因此永远无法从Map检索到。

默认情况下, Point的equals方法使用==即对象标识。 因为Hashmap使用equals方法,所以最终使用==比较密钥,除非它是同一个对象,否则返回false。

为了解决这个问题,实现Point的equalshashCode方法,以便如果坐标相同,则point1.equals(point2)返回true。

因为,您使用Point作为hashmap的键,您需要覆盖默认的equals和hashCode方法

未经检验的代码

 @Override public boolean equals(Object obj){ if (obj == null) return false; if (obj == this) return true; if (!(obj instanceof Point.class)) return false; Point point = (Point) obj; return this.x == point.x && this.y == point.y; }