当我创建一个对象的新实例作为关键字时,Java HashMap.get()返回null [英] Java HashMap.get() returns null when I create a new instance of an object as a key

查看:593
本文介绍了当我创建一个对象的新实例作为关键字时,Java HashMap.get()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  HashMap< Point,String> cache = new HashMap< Point,String>(); 
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; (int i = 1; i <= maxRow; i ++){
for(int j = 1; j <= maxCol; j ++){
System.out.print cache.get(new Point(i,j)));
if(j System.out.print(\t);
if(j == maxRow)
System.out.println();




$ b

这将返回:

  null null 
null null



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

解决方案

为了详细说明上面的注释,您的Point类应该实现hashcode和equals如下所示:
(存在许多实现,它只是一个工作)
假设你的实例的变量是 x y

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ;
if(o == null || getClass()!= o.getClass())return false;

Point point =(Point)o;

if(x!= point.x)return false;
if(y!= point.y)返回false;

返回true;
}

public int hashCode(){
int result = x;
结果= 31 *结果+ y;
返回结果;
}

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

 尽管合理实用,hashCode由
* class {@code Object}定义的方法确实为不同的
*对象返回不同的整数。 (这通常通过将对象的内部
*地址转换为一个整数来实现,但是这种实现
*技术不是
* Java所需的 *
* @返回此对象的哈希码值。
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
* /
public native int hashCode( );

因此, new Point(1,2)不会被视为等于新的Point(1,2),因此永远不能从 Map 。


I'm making a spreadsheet application and I'm using a HashMap to store the data in the cells. As the key I'm using a Point class, which only has the number of rows and colums. The problem I'm having is that if I use the HashMap.get() with a new Point, it returns a null value.

    HashMap<Point, String> cache = new HashMap<Point, String>();
    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("");
        }
    }

This returns:

null     null
null     null

I'm probably missing something obvious, but I can't find it myself. Also, if you happen to know if there is a better data structure for storing data from cells I'd love to hear it. Thanks in advance!

解决方案

To detail my comment above, your Point class should implement hashcode and equals like following: (many implementations exist, it's just one that works) Supposing that your instance's variables are x and y.

    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;
    }

Otherwise, if you don't override those methods, Object's Javadoc explains well your issue:

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
     * Java<font size="-2"><sup>TM</sup></font> 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();

Thus, new Point(1,2) would not be considered as equal to new Point(1,2) and therefore, could never be retrieved from your Map.

这篇关于当我创建一个对象的新实例作为关键字时,Java HashMap.get()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆