Java Hashmap 从对象键中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17579634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:22:40  来源:igfitidea点击:

Java Hashmap get value from object key

javahashmap

提问by user2150129

Beginner question: I have a hashmap which stores an array of integers as values. The key for each value is an object which consists of two integers (coordinate).

初学者问题:我有一个哈希图,它将整数数组存储为值。每个值的键是一个由两个整数(坐标)组成的对象。

My question: how can I retrieve a value from the hashmap, based on the two co?rdinates within my object (my 'key')?

我的问题:如何根据对象中的两个坐标(我的“键”)从哈希图中检索值?

My Coords Class (with a little help from Eclipse):

我的 Coords 类(在 Eclipse 的帮助下):

    public class Coords {
    int x;
    int y;

    public Coords(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

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

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Coords other = (Coords) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }

}

Building the Hashmap:

构建哈希图:

public class BuildMap {

public Coords coords;
public int[] someData = new int[4];
public Random random = new Random();
HashMap<Coords, int[]> map = new HashMap<Coords, int[]>();

public void buildHashMap() {

    // coordinates from (0,0) to (31,31)
    for (int i = 0; i < 32; i++) {
        for (int j = 0; j < 32; j++) {

            coords = new Coords(i, j);

            // Every Coord gets a few random numbers
            for (int k = 0; k < 4; k++) {
                someData[k] = random.nextInt(8564);
            }

            map.put(coords, someData);

        }
    }

If I want to access the array on coordinates 12,13, how can I retrieve it? Is iteration needed (I hope not, I want to add 100,000+ coordinates and quick access ofcourse).

如果我想访问坐标 12,13 上的数组,我该如何检索它?是否需要迭代(我希望不需要,我想添加 100,000 多个坐标和快速访问当然)。

I was hoping this would work somewhat in the line of

我希望这会在某种程度上起作用

int[] theValues = map.get(new Coords(12,13));

I hope you can help me. thanks in advance!

我希望你能帮助我。提前致谢!

回答by Jim Garrison

The problem is in how you construct the Map.

问题在于您如何构建 Map。

You are adding the same array as the value for each element.

您正在添加与每个元素的值相同的数组。

You need to instantiate a new array for each element.

您需要为每个元素实例化一个新数组。

for (int i = 0; i < 32; i++) {
    for (int j = 0; j < 32; j++) {

        coords = new Coords(i, j);
        int[] someData = new int[4];   // <==== create a new array for each Map value
        // Every Coord gets a few random numbers
        for (int k = 0; k < 4; k++) {
            someData[k] = random.nextInt(8564);
        }

        map.put(coords, someData);

    }

回答by jlordo

You have one mistake: you are only using one array, and many references to it.

你犯了一个错误:你只使用了一个数组,并且有很多对它的引用。

Move this line

移动这条线

public int[] someData = new int[4]; // without public 

above or below this line:

在这条线的上方或下方:

coords = new Coords(i, j);

to fix it.

要解决这个问题。