是否有可能实现一个HashMap有一个键和两个值。就像HashMap?
请帮助我,也通过告诉(如果没有办法)任何其他方法来实现三个值的存储与一个作为关键?
是否有可能实现一个HashMap有一个键和两个值。就像HashMap?
请帮助我,也通过告诉(如果没有办法)任何其他方法来实现三个值的存储与一个作为关键?
当前回答
你可以:
使用具有列表作为值的映射。地图< KeyType、列表< ValueType > >。 创建一个新的包装器类,并在映射中放置该包装器的实例。Map < KeyType, WrapperType >。 使用类似类的元组(节省了创建大量包装器)。映射<KeyType,元组<Value1Type, Value2Type>>。 同时使用多个地图。
例子
1. 以list为值的映射
// 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];
这种方法的缺点是列表没有绑定到恰好两个值。
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();
这种方法的缺点是必须为所有这些非常简单的容器类编写大量样板代码。
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;
在我看来,这是最好的解决办法。
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"];
这种解决方案的缺点是,两个映射之间的关联并不明显,编程错误可能会导致两个映射不同步。
其他回答
你可以隐式地做。
// Create the map. There is no restriction to the size that the array String can have
HashMap<Integer, String[]> map = new HashMap<Integer, String[]>();
//initialize a key chosing the array of String you want for your values
map.put(1, new String[] { "name1", "name2" });
//edit value of a key
map.get(1)[0] = "othername";
这是非常简单和有效的。 如果你想要不同类的值,你可以这样做:
HashMap<Integer, Object[]> map = new HashMap<Integer, Object[]>();
我无法回复Paul的评论,所以我在这里为Vidhya创建了新的评论:
Wrapper将是我们想要存储为值的两个类的超类。
在包装器类内部,我们可以将这些关联作为两个类对象的实例变量对象。
e.g.
class MyWrapper {
Class1 class1obj = new Class1();
Class2 class2obj = new Class2();
...
}
在HashMap中,我们可以这样写,
Map<KeyObject, WrapperObject>
WrapperObj将有类变量:class1Obj, class2Obj
我们可以创建一个类来拥有多个键或值,该类的对象可以用作map中的参数。 你可以参考https://stackoverflow.com/a/44181931/8065321
最简单的方法是使用谷歌集合库:
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class Test {
public static void main(final String[] args) {
// multimap can handle one key with a list of values
final Multimap<String, String> cars = ArrayListMultimap.create();
cars.put("Nissan", "Qashqai");
cars.put("Nissan", "Juke");
cars.put("Bmw", "M3");
cars.put("Bmw", "330E");
cars.put("Bmw", "X6");
cars.put("Bmw", "X5");
cars.get("Bmw").forEach(System.out::println);
// It will print the:
// M3
// 330E
// X6
// X5
}
}
Maven链接:https://mvnrepository.com/artifact/com.google.collections/google-collections/1.0-rc2
更多相关信息:http://tomjefferys.blogspot.be/2011/09/multimaps-google-guava.html
看一看guava库中的Multimap及其实现——HashMultimap
类似于Map的集合,但可以将多个值与单个键关联。如果使用相同的键但不同的值调用put(K, V)两次,则multimap包含从键到两个值的映射。