在 HashMap 中的单个键下存储多个值
Java 中的映射将键与单个值关联,但您可能会遇到需要在同一个键下存储多个值。本问题探讨如何实现此功能。
以列表作为值的映射
一种方法是使用值为列表的映射。这允许您将多个值存储为与键关联的列表。但是,这有一个限制,即列表可以包含任意数量的值,而不仅仅是两个。
使用包装类
您还可以创建一个单独的包装类保存此包装器的值并将其实例放置在地图中。这种方法需要定义一个类并为每个键创建实例,从而产生额外的代码。
基于元组的方法
如果您想强制执行固定数量的值,您可以使用元组类(通常在库中找到或通过实现您自己的)。然后,映射可以将元组存储为值,确保一个键下多个值的结构一致。
多个映射
另一种选择是使用多个映射,每个映射一个价值。这种方法很简单,但如果处理不当,可能会带来地图不同步的风险。
示例
以列表作为值的地图
import java.util.*; class Person { String name; public Person(String name) { this.name = name; } public String toString() { return this.name; } } class HashMapMultiple { public HashMapMultiple() { Map<String, List<Person>> peopleByForename = new HashMap<>(); List<Person> people = new ArrayList<>(); people.add(new Person("Bob Smith")); people.add(new Person("Bob Jones")); peopleByForename.put("Bob", people); List<Person> bobs = peopleByForename.get("Bob"); Person bob1 = bobs.get(0); Person bob2 = bobs.get(1); System.out.println("Bob1: " + bob1); System.out.println("Bob2: " + bob2); } public static void main(String[] args) { new HashMapMultiple(); } }
使用包装器类
import java.util.*; class Person { String name; public Person(String name) { this.name = name; } public String toString() { return this.name; } } 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; } class HashMapMultiple { public HashMapMultiple() { Map<String, Wrapper> peopleByForename = new HashMap<>(); peopleByForename.put("Bob", new Wrapper(new Person("Bob Smith"), new Person("Bob Jones"))); Wrapper bobs = peopleByForename.get("Bob"); Person bob1 = bobs.getPerson1(); Person bob2 = bobs.getPerson2(); System.out.println("Bob1: " + bob1); System.out.println("Bob2: " + bob2); } public static void main(String[] args) { new HashMapMultiple(); } }
基于元组的方法
此方法需要一个元组类:
import java.util.*; class Person { String name; public Person(String name) { this.name = name; } public String toString() { return this.name; } } class Tuple2<T1,T2> { public Tuple2(T1 val1, T2 val2) { this.val1 = val1; this.val2 = val2; } public T1 val1; public T2 val2; } class HashMapMultiple { public HashMapMultiple() { Map<String, Tuple2<Person, Person>> peopleByForename = new HashMap<>(); peopleByForename.put("Bob", new Tuple2<Person, Person>(new Person("Bob Smith"), new Person("Bob Jones"))); Tuple2<Person, Person> bobs = peopleByForename.get("Bob"); Person bob1 = bobs.val1; Person bob2 = bobs.val2; System.out.println("Bob1: " + bob1); System.out.println("Bob2: " + bob2); } public static void main(String[] args) { new HashMapMultiple(); } }
多个映射
import java.util.*; class Person { String name; public Person(String name) { this.name = name; } public String toString() { return this.name; } } class HashMapMultiple { public HashMapMultiple() { Map<String, Person> firstPersonByForename = new HashMap<>(); Map<String, Person> secondPersonByForename = new HashMap<>(); firstPersonByForename.put("Bob", new Person("Bob Smith")); secondPersonByForename.put("Bob", new Person("Bob Jones")); Person bob1 = firstPersonByForename.get("Bob"); Person bob2 = secondPersonByForename.get("Bob"); System.out.println("Bob1: " + bob1); System.out.println("Bob2: " + bob2); } public static void main(String[] args) { new HashMapMultiple(); } }
以上是如何在 Java HashMap 中的单个键下存储多个值?的详细内容。更多信息请关注PHP中文网其他相关文章!