带有多个键的哈希映射? [英] Hash map with multiple keys?

查看:117
本文介绍了带有多个键的哈希映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  HashMap< String,String,Integer>我可以在Java中使用哈希映射吗? hmap = new HashMap< String,String,Integer>()

我的问题与此处类似问题



我是Java的新手。所以我想知道的是,如果我需要类似上面的东西,那么最好的数据结构是什么,如果这是无效的呢?

解决方案

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

  public class MyKey {
private String a;
私人字符串b;

// ...访问器,修改器等
}



<然后将它的对象用作映射中的键:

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

稍后,添加一个新条目:

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


Can I have a hash map in Java that looks like this?

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

My question is similar to this one hereQuestion

I'm a newbie to Java. So what I want to know is, what would be the best data structure to use if I need something like above, if that is not valid?

解决方案

Create a simple class holding two String objects:

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

    // ... accessors, mutators etc.
}

And then use it's objects as keys in your map:

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

Later, to add a new entry:

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

这篇关于带有多个键的哈希映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆