- Guava 教程
- Guava - 首頁
- Guava - 概述
- Guava - 環境設定
- Guava - Optional 類
- Guava - Preconditions 類
- Guava - Ordering 類
- Guava - Objects 類
- Guava - Range 類
- Guava - Throwables 類
- Guava - 集合工具類
- Guava - 快取工具類
- Guava - 字串工具類
- Guava - 原語工具類
- Guava - 數學工具類
- Guava 有用資源
- Guava - 快速指南
- Guava - 有用資源
- Guava - 討論
Guava - 多值對映介面
Multimap 介面擴充套件了 Map,以便其鍵可以一次對映到多個值。
介面宣告
以下是com.google.common.collect.Multimap<K,V>介面的宣告:
@GwtCompatible public interface Multimap<K,V>
介面方法
| 序號 | 方法及描述 |
|---|---|
| 1 |
Map<K,Collection<V>> asMap() 將此多值對映作為從每個不同的鍵到該鍵關聯值的非空集合的 Map 的檢視返回。 |
| 2 |
void clear() 刪除多值對映中的所有鍵值對,使其為空。 |
| 3 |
boolean containsEntry(Object key, Object value) 如果此多值對映包含至少一個具有該鍵和該值的鍵值對,則返回 true。 |
| 4 |
boolean containsKey(Object key) 如果此多值對映包含至少一個具有該鍵的鍵值對,則返回 true。 |
| 5 | boolean containsValue(Object value) 如果此多值對映包含至少一個具有該值的鍵值對,則返回 true。 |
| 6 |
Collection<Map.Entry<K,V>> entries() 返回此多值對映中包含的所有鍵值對的檢視集合,作為 Map.Entry 例項。 |
| 7 |
boolean equals(Object obj) 將指定的物件與此多值對映進行相等性比較。 |
| 8 |
Collection<V> get(K key) 返回與此多值對映中鍵關聯的值的檢視集合(如果存在)。 |
| 9 |
int hashCode() 返回此多值對映的雜湊碼。 |
| 10 |
boolean isEmpty() 如果此多值對映不包含任何鍵值對,則返回 true。 |
| 11 |
Multiset<K> keys() 返回一個檢視集合,其中包含此多值對映中每個鍵值對的鍵,不會摺疊重複項。 |
| 12 |
Set<K> keySet() 返回此多值對映中包含的所有不同鍵的檢視集合。 |
| 13 |
boolean put(K key, V value) 在此多值對映中儲存鍵值對。 |
| 14 |
boolean putAll(K key, Iterable<? extends V> values) 為此多值對映中的每個 values 儲存鍵值對,所有這些值都使用相同的鍵 key。 |
| 15 |
boolean putAll(Multimap<? extends K,? extends V> multimap) 按 multimap.entries() 返回的順序在此多值對映中儲存 multimap 的所有鍵值對。 |
| 16 |
boolean remove(Object key, Object value) 從此多值對映中刪除一個具有該鍵和該值的鍵值對(如果存在)。 |
| 17 |
Collection<V> removeAll(Object key) 刪除與該鍵關聯的所有值。 |
| 18 |
Collection<V> replaceValues(K key, Iterable<? extends V> values) 使用相同的鍵儲存值的集合,替換該鍵的任何現有值。 |
| 19 |
int size() 返回此多值對映中的鍵值對數。 |
| 20 |
Collection<V> values() 返回一個檢視集合,其中包含此多值對映中每個鍵值對的值,不會摺疊重複項 (因此 values().size() == size())。 |
Multimap 示例
使用您選擇的任何編輯器在例如C:/> Guava目錄下建立以下 Java 程式。
GuavaTester.java
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
Multimap<String,String> multimap = tester.getMultimap();
List<String> lowerList = (List<String>)multimap.get("lower");
System.out.println("Initial lower case list");
System.out.println(lowerList.toString());
lowerList.add("f");
System.out.println("Modified lower case list");
System.out.println(lowerList.toString());
List<String> upperList = (List<String>)multimap.get("upper");
System.out.println("Initial upper case list");
System.out.println(upperList.toString());
upperList.remove("D");
System.out.println("Modified upper case list");
System.out.println(upperList.toString());
Map<String, Collection<String>> map = multimap.asMap();
System.out.println("Multimap as a map");
for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
String key = entry.getKey();
Collection<String> value = multimap.get("lower");
System.out.println(key + ":" + value);
}
System.out.println("Keys of Multimap");
Set<String> keys = multimap.keySet();
for(String key:keys) {
System.out.println(key);
}
System.out.println("Values of Multimap");
Collection<String> values = multimap.values();
System.out.println(values);
}
private Multimap<String,String> getMultimap() {
//Map<String, List<String>>
// lower -> a, b, c, d, e
// upper -> A, B, C, D
Multimap<String,String> multimap = ArrayListMultimap.create();
multimap.put("lower", "a");
multimap.put("lower", "b");
multimap.put("lower", "c");
multimap.put("lower", "d");
multimap.put("lower", "e");
multimap.put("upper", "A");
multimap.put("upper", "B");
multimap.put("upper", "C");
multimap.put("upper", "D");
return multimap;
}
}
驗證結果
使用javac編譯器編譯該類,如下所示:
C:\Guava>javac GuavaTester.java
現在執行 GuavaTester 以檢視結果。
C:\Guava>java GuavaTester
檢視結果。
Initial lower case list [a, b, c, d, e] Modified lower case list [a, b, c, d, e, f] Initial upper case list [A, B, C, D] Modified upper case list [A, B, C] Multimap as a map upper:[a, b, c, d, e, f] lower:[a, b, c, d, e, f] Keys of Multimap upper lower Values of Multimap [a, b, c, d, e, f, A, B, C]