简体   繁体   中英

java ConcurrentHashMap with constructor as key

I am currently trying to write a little game in java lwjgl/OpenGL.

When running the code i get the value NULL when reading from some ConcurrentHashMap. 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. It's basically a class that contains a ConcurrentHashMap and two methods to access it:

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 . This class just contains the three Integers x, y and z, and a Constructer to set these values.

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". 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); So obviously the ConcurrentHashMap can't give "Just a String" back, because it's not the same ValueContainer, so it gives NULL. Here's the code of the Main class with this little modification:

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 .

Please take a look add the API-documentation of the two methods. API

Maybe you use a IDE like Ecplise oder IntelliJ which will help you with the details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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