简体   繁体   中英

Hashmap with 2 keys

I would like to create a hashmap with 2 keys. How would I go about doing this?

Let's say the hashmap instead of having 1 key to find a value needs 1 key and then uses the other key to narrow down the search to a certain type.

I need it to use a player that we give it. That would be key1. For key2 I need it to be a string for a type which could be something like "fly" or "speed" and then I need to get a value from those.

I would also not like to use more than 1 class for this job.

It would be nice if I could do HashMap but you can't do that directly.

I have already searched and I am still learning some java and still reading the oracle java handbook so keep that in mind because of I'm not sure if this is a stupid question or not.

Also, I have searched for an answer but I have tried looking into questions and answers of other users but that has not helped my problem.

You can do it by using two map as follows:

Map<Kay1DataType, Map<Kay2DataType, V>> map = //...
//...

Then to access you can do as follows:

map.get(Key1).get(Key2);

Then to put the value you can do like follows:

Map<Kay2DataType, V> secondMap = //...
secondMap.put(Key2, /* Object of type V*/);

Finally put to the map

map.put(key1, secondMap);

The question wasn't entirely clear, but I'll give this my best shot...

The only logical way that you could go about doing this (given that it were a HashMap with the same types for both key and value), you could have one key have a value that would act as the key for the second value, which was the desired value. I'll write a quick example (because I know this seems a little confusing when I try to explain it in writing.) If you have any questions, feel free to comment.

public class Example {
    static HashMap<String, String> map = new HashMap<>();
    public static void main(String[] args) {
        //The value from the first pair is the key for the second.
        map.put("Key 1", "Value 1");
        map.put("Value 1", "Value 2"); 

        //Prints: Value 2
        System.out.println(map.get(map.get("Key 1")));
    }
}

If you absolutely had to make the key/values different, the only way to do this would be two different maps, kind of following the same process:

public class Example {
    static HashMap<String, String> map = new HashMap<>();
    static HashMap<String, Integer> secondMap = new HashMap();
    public static void main(String[] args) {
        //The value from the first pair is the key for the second.
        map.put("Key 1", "Value 1");
        map.put("Value 1", 5); 

        //Prints: 5
        System.out.println(map.get(map.get("Key 1")));
    }
}

Hope this helps!

Try using a Map as the value for the first Map . Example:

final Map<String,Map<String,String>> doubleKeyMap = new HashMap<>();
final Map<String,String> secondMap = new HashMap<>();
secondMap.put(secondKey, somevalue);
doubleKeyMap.put(mainKey, secondMap);

final String value = doubleKeyMap.get(firstKey).get(secondKey);

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