HashMap 在同一键下具有多个值 [英] HashMap with multiple values under the same key

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

问题描述

我们有没有可能实现一个一键二值的HashMap.就像 HashMap 一样?

Is it possible for us to implement a HashMap with one key and two values. Just as HashMap?

请帮助我,也请告诉(如果没有办法)任何其他方式来实现以一个为键的三个值的存储?

Please do help me, also by telling (if there is no way) any other way to implement the storage of three values with one as the key?

推荐答案

您可以:

  1. 使用具有列表作为值的地图.Map>.
  2. 创建一个新的包装器类并将此包装器的实例放置在地图中.Map.
  3. 使用类似类的元组(节省创建大量包装器).Map>.
  4. 并排使用多个地图.


示例

1.以列表为值映射

// create our map
Map<String, List<Person>> peopleByForename = new HashMap<>();    

// populate it
List<Person> people = new ArrayList<>();
people.add(new Person("Bob Smith"));
people.add(new Person("Bob Jones"));
peopleByForename.put("Bob", people);

// read from it
List<Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs[0];
Person bob2 = bobs[1];

这种方法的缺点是列表不能正好绑定两个值.

The disadvantage with this approach is that the list is not bound to exactly two values.

2.使用包装类

// define our wrapper
class Wrapper {
    public Wrapper(Person person1, Person person2) {
       this.person1 = person1;
       this.person2 = person2;
    }

    public Person getPerson1 { return this.person1; }
    public Person getPerson2 { return this.person2; }

    private Person person1;
    private Person person2;
}

// create our map
Map<String, Wrapper> peopleByForename = new HashMap<>();

// populate it
peopleByForename.put("Bob", new Wrapper(new Person("Bob Smith"),
                                        new Person("Bob Jones"));

// read from it
Wrapper bobs = peopleByForename.get("Bob");
Person bob1 = bobs.getPerson1;
Person bob2 = bobs.getPerson2;

这种方法的缺点是您必须为所有这些非常简单的容器类编写大量样板代码.

The disadvantage to this approach is that you have to write a lot of boiler-plate code for all of these very simple container classes.

3.使用元组

// you'll have to write or download a Tuple class in Java, (.NET ships with one)

// create our map
Map<String, Tuple2<Person, Person> peopleByForename = new HashMap<>();

// populate it
peopleByForename.put("Bob", new Tuple2(new Person("Bob Smith",
                                       new Person("Bob Jones"));

// read from it
Tuple<Person, Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs.Item1;
Person bob2 = bobs.Item2;

我认为这是最好的解决方案.

This is the best solution in my opinion.

4.多张地图

// create our maps
Map<String, Person> firstPersonByForename = new HashMap<>();
Map<String, Person> secondPersonByForename = new HashMap<>();

// populate them
firstPersonByForename.put("Bob", new Person("Bob Smith"));
secondPersonByForename.put("Bob", new Person("Bob Jones"));

// read from them
Person bob1 = firstPersonByForename["Bob"];
Person bob2 = secondPersonByForename["Bob"];

这种解决方案的缺点是两个地图相关性并不明显,程序错误可能会导致两个地图不同步.

The disadvantage of this solution is that it's not obvious that the two maps are related, a programmatic error could see the two maps get out of sync.

这篇关于HashMap 在同一键下具有多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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