This question already has answers here:
How to create a HashMap with two keys (Key-Pair, Value)?
                                
                                    (12个答案)
                                
                        
                3年前关闭。
            
        

我可以在Java中使用如下所示的哈希图吗?

HashMap<String, String, Integer> hmap = new HashMap<String, String, Integer>()


我的问题类似于这里的这个问题Question

我是Java的新手。因此,我想知道的是,如果我需要上面类似的内容(如果无效),将使用的最佳数据结构是什么?

最佳答案

创建一个包含两个String对象的简单类:

public class MyKey {
    private String a;
    private String b;

    // ... accessors, mutators etc.
}


然后将其对象用作地图中的键:

HashMap<MyKey, Integer> hmap = new HashMap<>()


以后,添加一个新条目:

hmap.put(new MyKey("a", "b"), 2);

07-28 04:04
查看更多