简体   繁体   English

带有2个键的哈希图

[英]Hashmap with 2 keys

I would like to create a hashmap with 2 keys. 我想用2个键创建一个hashmap。 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. 假设哈希图不是用1个键来查找值,而是需要1个键,然后使用另一个键将搜索范围缩小到某种类型。

I need it to use a player that we give it. 我需要它来使用我们提供的播放器。 That would be key1. 那将是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. 对于key2,我需要将它作为类型的字符串,该类型可以是“ fly”或“ speed”之类的东西,然后我需要从中获取一个值。

I would also not like to use more than 1 class for this job. 我也不想为这个工作使用超过1个班级。

It would be nice if I could do HashMap but you can't do that directly. 如果我可以做HashMap,但您不能直接做,那就太好了。

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. 我已经搜索过,并且仍在学习一些Java,并且仍在阅读Oracle Java手册,因此请记住这一点,因为我不确定这是否是一个愚蠢的问题。

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

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. 您可以执行此操作的唯一逻辑方法(假设它是HashMap,并且键和值的类型相同),则可以让一个键的值充当第二个值的键,即所需的值。 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 . 尝试使用Map作为第一价值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);

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

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