简体   繁体   English

java ConcurrentHashMap 以构造函数为键

[英]java ConcurrentHashMap with constructor as key

I am currently trying to write a little game in java lwjgl/OpenGL.我目前正在尝试用 java lwjgl/OpenGL 编写一个小游戏。

When running the code i get the value NULL when reading from some ConcurrentHashMap.运行代码时,我从某些 ConcurrentHashMap 读取时得到值 NULL。 I've written a simple program to reproduce the same issue and sure enough, i could.我写了一个简单的程序来重现同样的问题,果然,我可以。

Let me show the code to you: The Program consists of three classes.让我向您展示代码: 该程序由三个类组成。 The Main class:类:

package main;

public class Main {

    private MapContainer con = new MapContainer();

    public static void main(String[] args) {

        new Main();

    }

    public Main() {

        ValueContainer vc = new ValueContainer(1, 2, 3);
        this.con.set(vc, "Just a String");
        System.out.println(this.con.get(vc));

    }

}

Then there's the MapContainer class.然后是MapContainer类。 It's basically a class that contains a ConcurrentHashMap and two methods to access it:它基本上是一个包含 ConcurrentHashMap 和两个访问它的方法的类:

package main;

import java.util.concurrent.ConcurrentHashMap;

public class MapContainer {

    private ConcurrentHashMap<ValueContainer, String> map = new ConcurrentHashMap<>();

    public void set(ValueContainer key, String value) {
        this.map.put(key, value);
    }

    public String get(ValueContainer key) {
        return this.map.get(key);
    }

}

At last, there's the ValueContainer .最后,还有ValueContainer This class just contains the three Integers x, y and z, and a Constructer to set these values.这个类只包含三个整数 x、y 和 z,以及一个用于设置这些值的构造函数。

package main;

public class ValueContainer {

    public ValueContainer(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public int x, y, z;

}

So when i run the main class, i create a new ValueContainer with the values 1, 2, 3 and put it into the map Container, along with the String "Just a String".因此,当我运行主类时,我创建了一个新的 ValueContainer,其值为 1、2、3,并将其与字符串“Just a String”一起放入地图容器中。 Then i read the String with that exact Value container and print it out.然后我用那个确切的值容器读取字符串并将其打印出来。 Sure enough the program works and i get "Just a String" printed in the Console.果然程序可以工作,我在控制台中打印了“只是一个字符串”。

So now there's my game: In my game i have to access a similar ConcurrentHashMap, but i cant use the same ValueContainer to access the String, but i have to create a new one with new ValueContainer(1, 2, 3);所以现在是我的游戏:在我的游戏中,我必须访问一个类似的 ConcurrentHashMap,但我不能使用相同的 ValueContainer 来访问 String,但我必须使用new ValueContainer(1, 2, 3);创建一个新的new ValueContainer(1, 2, 3); So obviously the ConcurrentHashMap can't give "Just a String" back, because it's not the same ValueContainer, so it gives NULL.所以显然 ConcurrentHashMap 不能返回“Just a String”,因为它不是同一个 ValueContainer,所以它给出了 NULL。 Here's the code of the Main class with this little modification:这是带有这个小修改的Main类的代码:

package main;

public class Main {

    private MapContainer con = new MapContainer();

    public static void main(String[] args) {

        new Main();

    }

    public Main() {

        this.con.set(new ValueContainer(1, 2, 3), "Just a String");
        System.out.println(this.con.get(new ValueContainer(1, 2, 3)));

    }

}

Now my question:现在我的问题:

Is there any way for me to use the version in the second main class, but without the issue, so that i get printed out "Just a String" in Console?有什么方法可以让我在第二个主类中使用该版本,但没有问题,以便我在控制台中打印出“只是一个字符串”?

Thank you.谢谢你。

Yes, quite simple.是的,很简单。

You have to override the two methods Object.hashCode() and Object.equals() in your class ValueContainer .您必须在ValueContainer类中覆盖Object.hashCode()Object.equals()两个方法。

Please take a look add the API-documentation of the two methods.请查看添加这两种方法的 API 文档。 API 应用程序接口

Maybe you use a IDE like Ecplise oder IntelliJ which will help you with the details.也许您使用像 Ecplise 或 IntelliJ 这样的 IDE,它可以帮助您了解细节。

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

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