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

查看:217
本文介绍了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< KeyType,List< ValueType>>

  2. 创建一个新的包装类并将此包装器的实例放在地图中。 Map< KeyType,WrapperType>

  3. 使用类似的元组(保存创建大量包装器)。 Map< KeyType,Tuple< Value1Type,Value2Type>>

  4. 并排使用多个地图。

  1. Use a map that has a list as the value. Map<KeyType, List<ValueType>>.
  2. Create a new wrapper class and place instances of this wrapper in the map. Map<KeyType, WrapperType>.
  3. Use a tuple like class (saves creating lots of wrappers). Map<KeyType, Tuple<Value1Type, Value2Type>>.
  4. Use mulitple maps side-by-side.






示例



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
Wrapper people = new Wrapper()
peopleByForename.put("Bob", new Wrapper(new Person("Bob Smith"),
                                        new Person("Bob Jones"));

// read from it
Wrapper bobs = peopleByForename["Bob"];
Person bob1 = bobs.Person1;
Person bob2 = bobs.Person2;

这种方法的缺点是你必须写所有这些非常简单的容器类的很多样板代码。

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天全站免登陆