在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中文網其他相關文章!