Interface Multimap<K,V>
- Type Parameters:
K- key type | 键类型V- value type | 值类型
- All Known Subinterfaces:
ListMultimap<K,V>, SetMultimap<K, V>, SortedSetMultimap<K, V>
- All Known Implementing Classes:
AbstractMultimap, ArrayListMultimap, HashSetMultimap, ImmutableListMultimap, ImmutableMultimap, ImmutableSetMultimap, LinkedHashMultimap, TreeSetMultimap
public interface Multimap<K,V>
Multimap - Map with multiple values per key
Multimap - 每个键有多个值的映射
A map-like collection where each key can be associated with multiple values. The collection of values for each key can be a List, Set, or other Collection type.
类似映射的集合,每个键可以关联多个值。每个键的值集合可以是 List、Set 或其他 Collection 类型。
Features | 主要功能:
- Multiple values per key - 每个键多个值
- Key set view - 键集合视图
- Values collection view - 值集合视图
- Entries collection view - 条目集合视图
- Map view (key to collection) - 映射视图(键到集合)
Usage Examples | 使用示例:
// Create Multimap - 创建 Multimap
Multimap<String, Integer> multimap = ArrayListMultimap.create();
// Put values - 放入值
multimap.put("a", 1);
multimap.put("a", 2);
multimap.put("b", 3);
// Get values - 获取值
Collection<Integer> values = multimap.get("a"); // [1, 2]
// Check contains - 检查包含
boolean hasKey = multimap.containsKey("a"); // true
boolean hasValue = multimap.containsValue(1); // true
boolean hasEntry = multimap.containsEntry("a", 1); // true
// Remove - 移除
multimap.remove("a", 1); // removes single entry
multimap.removeAll("a"); // removes all values for key
Performance | 性能特性:
- put: O(1) for hash-based - put: O(1) 基于哈希
- get: O(1) for hash-based - get: O(1) 基于哈希
- containsKey: O(1) for hash-based - containsKey: O(1) 基于哈希
- containsValue: O(n) - containsValue: O(n)
Security | 安全性:
- Thread-safe: Implementation dependent - 线程安全: 取决于实现
- Null-safe: Implementation dependent - 空值安全: 取决于实现
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionMap<K, ? extends Collection<V>> asMap()Return a map view with key to collection mapping.voidclear()Clear all entries.booleancontainsEntry(Object key, Object value) Check if the multimap contains the key-value pair.booleancontainsKey(Object key) Check if the multimap contains the key.booleancontainsValue(Object value) Check if the multimap contains the value.entries()Return a collection view of all key-value pairs.Return the collection of values for the key.booleanisEmpty()Check if the multimap is empty.keys()Return a multiset view of the keys (includes duplicates by value count).keySet()Return a set view of the keys.booleanStore a key-value pair.default voidStore all key-value pairs from another multimap.default voidStore key-value pairs from a map.booleanStore multiple values for a key.booleanRemove a single key-value pair.Remove all values for a key.replaceValues(K key, Iterable<? extends V> values) Replace all values for a key.intsize()Return the total number of key-value pairs.values()Return a collection view of all values.
-
Method Details
-
size
int size()Return the total number of key-value pairs. 返回键值对的总数。- Returns:
- size | 大小
-
isEmpty
boolean isEmpty()Check if the multimap is empty. 检查多重映射是否为空。- Returns:
- true if empty | 如果为空则返回 true
-
containsKey
Check if the multimap contains the key. 检查多重映射是否包含键。- Parameters:
key- the key | 键- Returns:
- true if contains | 如果包含则返回 true
-
containsValue
Check if the multimap contains the value. 检查多重映射是否包含值。- Parameters:
value- the value | 值- Returns:
- true if contains | 如果包含则返回 true
-
containsEntry
-
put
-
putAll
-
putAll
-
putAll
-
get
Return the collection of values for the key. 返回键的值集合。- Parameters:
key- the key | 键- Returns:
- values collection (never null, may be empty) | 值集合(不为 null,可能为空)
-
remove
-
removeAll
Remove all values for a key. 移除键的所有值。- Parameters:
key- the key | 键- Returns:
- removed values | 移除的值
-
replaceValues
Replace all values for a key. 替换键的所有值。- Parameters:
key- the key | 键values- the new values | 新值- Returns:
- previous values | 之前的值
-
clear
void clear()Clear all entries. 清除所有条目。 -
keySet
-
keys
-
values
Collection<V> values()Return a collection view of all values. 返回所有值的集合视图。- Returns:
- values collection | 值集合
-
entries
Collection<Map.Entry<K,V>> entries()Return a collection view of all key-value pairs. 返回所有键值对的集合视图。- Returns:
- entries collection | 条目集合
-
asMap
Map<K, ? extends Collection<V>> asMap()Return a map view with key to collection mapping. 返回键到集合映射的视图。- Returns:
- map view | 映射视图
-