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 Type
    Method
    Description
    Map<K, ? extends Collection<V>>
    Return a map view with key to collection mapping.
    void
    Clear all entries.
    boolean
    Check if the multimap contains the key-value pair.
    boolean
    Check if the multimap contains the key.
    boolean
    Check if the multimap contains the value.
    Return a collection view of all key-value pairs.
    get(K key)
    Return the collection of values for the key.
    boolean
    Check if the multimap is empty.
    Return a multiset view of the keys (includes duplicates by value count).
    Return a set view of the keys.
    boolean
    put(K key, V value)
    Store a key-value pair.
    default void
    putAll(Multimap<? extends K, ? extends V> multimap)
    Store all key-value pairs from another multimap.
    default void
    putAll(Map<? extends K, ? extends V> map)
    Store key-value pairs from a map.
    boolean
    putAll(K key, Iterable<? extends V> values)
    Store multiple values for a key.
    boolean
    remove(Object key, Object value)
    Remove a single key-value pair.
    Remove all values for a key.
    replaceValues(K key, Iterable<? extends V> values)
    Replace all values for a key.
    int
    Return the total number of key-value pairs.
    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

      boolean containsKey(Object key)
      Check if the multimap contains the key. 检查多重映射是否包含键。
      Parameters:
      key - the key | 键
      Returns:
      true if contains | 如果包含则返回 true
    • containsValue

      boolean containsValue(Object value)
      Check if the multimap contains the value. 检查多重映射是否包含值。
      Parameters:
      value - the value | 值
      Returns:
      true if contains | 如果包含则返回 true
    • containsEntry

      boolean containsEntry(Object key, Object value)
      Check if the multimap contains the key-value pair. 检查多重映射是否包含键值对。
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      true if contains | 如果包含则返回 true
    • put

      boolean put(K key, V value)
      Store a key-value pair. 存储键值对。
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      true if changed | 如果发生更改则返回 true
    • putAll

      default void putAll(Map<? extends K, ? extends V> map)
      Store key-value pairs from a map. 从映射存储键值对。
      Parameters:
      map - the map | 映射
    • putAll

      default void putAll(Multimap<? extends K, ? extends V> multimap)
      Store all key-value pairs from another multimap. 从另一个多重映射存储所有键值对。
      Parameters:
      multimap - the multimap | 多重映射
    • putAll

      boolean putAll(K key, Iterable<? extends V> values)
      Store multiple values for a key. 为键存储多个值。
      Parameters:
      key - the key | 键
      values - the values | 值
      Returns:
      true if changed | 如果发生更改则返回 true
    • get

      Collection<V> get(K key)
      Return the collection of values for the key. 返回键的值集合。
      Parameters:
      key - the key | 键
      Returns:
      values collection (never null, may be empty) | 值集合(不为 null,可能为空)
    • remove

      boolean remove(Object key, Object value)
      Remove a single key-value pair. 移除单个键值对。
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      true if removed | 如果移除则返回 true
    • removeAll

      Collection<V> removeAll(Object key)
      Remove all values for a key. 移除键的所有值。
      Parameters:
      key - the key | 键
      Returns:
      removed values | 移除的值
    • replaceValues

      Collection<V> replaceValues(K key, Iterable<? extends V> values)
      Replace all values for a key. 替换键的所有值。
      Parameters:
      key - the key | 键
      values - the new values | 新值
      Returns:
      previous values | 之前的值
    • clear

      void clear()
      Clear all entries. 清除所有条目。
    • keySet

      Set<K> keySet()
      Return a set view of the keys. 返回键的集合视图。
      Returns:
      key set | 键集合
    • keys

      Multiset<K> keys()
      Return a multiset view of the keys (includes duplicates by value count). 返回键的多重集视图(按值计数包含重复)。
      Returns:
      key multiset | 键多重集
    • 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 | 映射视图