1

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.

LoopTurn
  • 107
  • 2
  • 5
  • https://stackoverflow.com/questions/14677993/how-to-create-a-hashmap-with-two-keys-key-pair-value –  Apr 02 '18 at 01:59
  • The current two answers seem to solve your problem, however it's worth thinking about whether you really need a 2nd key - would you be better creating an object type, with attributes like "fly" and "speed" (I have no idea of what you're actually working on, it's just always a good idea to think about if your solution is the best way, or a hack). – Sean Apr 02 '18 at 02:10

4 Answers4

1

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);
I. Ahmed
  • 2,438
  • 1
  • 12
  • 29
0

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!

0

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);
-1

I think you could just somehow combine the 2 keys into 1 key. Either you could do that by combining their .toString() or .hashCode(). Or you could create an entirely new object with the 2 keys as variables.

ope
  • 67
  • 1
  • 6