简体   繁体   English

使用自定义Pair对象作为键访问HashMap值

[英]Accessing a HashMap value with a custom Pair Object as a key

I'm new to Java. 我是Java的新手。 I'm writing a 2d game and I've decided to use a HashMap to store my map data because I need to support negative indices for my map coordinates. 我正在写一个2D游戏,我决定使用HashMap来存储我的地图数据,因为我需要支持我的地图坐标的负指数。 This is because the size of the map can grow during the game. 这是因为地图的大小可以在游戏期间增长。

I have written a custom Pair class that stores final x and y values. 我编写了一个自定义Pair类,用于存储最终的x和y值。 I am using this Pair object as my key for my HashMap. 我使用这个Pair对象作为我的HashMap的关键。 My values are instances of a custom Cell class. 我的值是自定义Cell类的实例。

I have declared my HashMap as follows: 我已经将HashMap声明如下:

HashMap<Pair, Cell> tiles = new HashMap<Pair, Cell>();

Next, I add entries to my map using: 接下来,我使用以下命令将条目添加到地图:

tiles.put(new Pair(0,0), new Cell());

0,0 is obviously my unique x,y coordinate for this cell. 0,0显然是这个单元格的唯一x,y坐标。

How do I access the fields and methods of Cell using the .get() method of HashMap, specific to an individual Pair? 如何使用HashMap的.get()方法访问Cell的字段和方法,特定于单个Pair? Such as Pair(0,0) or Pair(0,1). 例如Pair(0,0)或Pair(0,1)。 If the key was simply a string or an int, I would have no problem. 如果键只是一个字符串或一个int,我就没有问题。 I just can't figure out how to format my key for an object with a specific coordinate. 我只是无法弄清楚如何格式化具有特定坐标的对象的键。

You need to override the equals and hashCode method of the Pair class. 您需要覆盖Pair类的equalshashCode方法。 Right now, if you have two instances of Pair as such: 现在,如果您有两个Pair实例:

Pair p1 = new Pair(0,0);
Pair p2 = new Pair(0,0);

These two instances in your program would not be seen as equal, and hence if you said: 您程序中的这两个实例不会被视为相等,因此如果您说:

tiles.put(p1, XXX);
tiles.put(p2, YYY);

the behavior would be such that your map would have two distinct keys, with two distinct values - where I believe what you would want is one key, with the last value YYY after these statements execute. 行为将是这样的,你的地图将有两个不同的键,有两个不同的值 - 我相信你想要的是一个键,这些语句执行后的最后一个值为YYY。

After implementing hashCode and equals , you could write a static helper method that instantiates a new Pair with some given coordinates, and performs a map lookup: 在实现hashCodeequals ,您可以编写一个静态帮助器方法,该方法实例化具有某些给定坐标的新Pair ,并执行映射查找:

static Cell lookup(int x, int y) {
    return tiles.get(new Pair(x, y));
}

Here's a basic Pair class to help you get started: 这是一个基本的Pair类,可以帮助您入门:

public class Pair {

    private final int x;
    private final int y;

    public Pair(final int x, final int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Pair)) {
            return false;
        }

        final Pair pair = (Pair) o;

        if (x != pair.x) {
            return false;
        }
        if (y != pair.y) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = x;
        result = 31 * result + y;
        return result;
    }
}

你只需要编写tiles.get(new Pair(0, 0)) ,就像你为put做的那样。

You will need to override the methods .equals() and .hashCode() for your Pair() type. 您需要覆盖Pair()类型的方法.equals().hashCode() They are needed in order to use a type in a HashMap. 为了在HashMap中使用类型,需要它们。 Equals can just check that the 2 values are equal: 等于可以检查2个值是否相等:

@Override public boolean equals(Object o) {
    if(this == o) return true;
    if(!(o instanceof Pair)) return false;
    Pair p = (Pair)o;
    return p.x == x && p.y == y;
}

For hashCode, you must generate a value that is unique: 对于hashCode,您必须生成一个唯一的值:

private volatile int hashCode; // define this as a member of the class

@Override public int hashCode() {
    int result = hashCode;
    if(result == 0) {
        result = 17 * x + 31 * y;
        hashCode = result;
    }
    return result;
}

Then you can access the cell at (0, 0) by simply calling 然后,您只需调用即可访问(0,0)处的单元格

tiles.get(new Pair(0, 0));

You need to override hashCode(). 您需要覆盖hashCode()。 Something like: 就像是:

public int hashCode() {
    return 17 * this.key + 31 * this.value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM