Class ImmutableMultimap<K,V>

java.lang.Object
cloud.opencode.base.collections.immutable.ImmutableMultimap<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型
All Implemented Interfaces:
Multimap<K,V>, Serializable
Direct Known Subclasses:
ImmutableListMultimap, ImmutableSetMultimap

public abstract class ImmutableMultimap<K,V> extends Object implements Multimap<K,V>, Serializable
ImmutableMultimap - Immutable Multimap Implementation ImmutableMultimap - 不可变多值映射实现

A multimap that cannot be modified after creation. Each key can map to multiple values.

创建后不能修改的多值映射。每个键可以映射到多个值。

Features | 主要功能:

  • Immutable - 不可变
  • Thread-safe - 线程安全
  • Multiple values per key - 每个键多个值

Usage Examples | 使用示例:

// Create using builder - 使用构建器创建
ImmutableMultimap<String, Integer> multimap = ImmutableMultimap.<String, Integer>builder()
    .put("a", 1)
    .put("a", 2)
    .put("b", 3)
    .build();

Collection<Integer> values = multimap.get("a"); // [1, 2]

Performance | 性能特性:

  • get: O(1) - get: O(1)
  • containsKey: O(1) - containsKey: O(1)

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Yes (nulls not allowed) - 空值安全: 是(不允许空值)
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • copyOf

      public static <K,V> ImmutableMultimap<K,V> copyOf(Multimap<? extends K, ? extends V> multimap)
      Copy from a multimap. 从多值映射复制。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      multimap - the multimap | 多值映射
      Returns:
      immutable multimap | 不可变多值映射
    • size

      public int size()
      Description copied from interface: Multimap
      Return the total number of key-value pairs. 返回键值对的总数。
      Specified by:
      size in interface Multimap<K,V>
      Returns:
      size | 大小
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Multimap
      Check if the multimap is empty. 检查多重映射是否为空。
      Specified by:
      isEmpty in interface Multimap<K,V>
      Returns:
      true if empty | 如果为空则返回 true
    • containsKey

      public boolean containsKey(Object key)
      Description copied from interface: Multimap
      Check if the multimap contains the key. 检查多重映射是否包含键。
      Specified by:
      containsKey in interface Multimap<K,V>
      Parameters:
      key - the key | 键
      Returns:
      true if contains | 如果包含则返回 true
    • containsValue

      public boolean containsValue(Object value)
      Description copied from interface: Multimap
      Check if the multimap contains the value. 检查多重映射是否包含值。
      Specified by:
      containsValue in interface Multimap<K,V>
      Parameters:
      value - the value | 值
      Returns:
      true if contains | 如果包含则返回 true
    • containsEntry

      public boolean containsEntry(Object key, Object value)
      Description copied from interface: Multimap
      Check if the multimap contains the key-value pair. 检查多重映射是否包含键值对。
      Specified by:
      containsEntry in interface Multimap<K,V>
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      true if contains | 如果包含则返回 true
    • get

      public abstract Collection<V> get(K key)
      Description copied from interface: Multimap
      Return the collection of values for the key. 返回键的值集合。
      Specified by:
      get in interface Multimap<K,V>
      Parameters:
      key - the key | 键
      Returns:
      values collection (never null, may be empty) | 值集合(不为 null,可能为空)
    • keySet

      public Set<K> keySet()
      Description copied from interface: Multimap
      Return a set view of the keys. 返回键的集合视图。
      Specified by:
      keySet in interface Multimap<K,V>
      Returns:
      key set | 键集合
    • keys

      public Multiset<K> keys()
      Description copied from interface: Multimap
      Return a multiset view of the keys (includes duplicates by value count). 返回键的多重集视图(按值计数包含重复)。
      Specified by:
      keys in interface Multimap<K,V>
      Returns:
      key multiset | 键多重集
    • values

      public Collection<V> values()
      Description copied from interface: Multimap
      Return a collection view of all values. 返回所有值的集合视图。
      Specified by:
      values in interface Multimap<K,V>
      Returns:
      values collection | 值集合
    • entries

      public Collection<Map.Entry<K,V>> entries()
      Description copied from interface: Multimap
      Return a collection view of all key-value pairs. 返回所有键值对的集合视图。
      Specified by:
      entries in interface Multimap<K,V>
      Returns:
      entries collection | 条目集合
    • asMap

      public abstract Map<K, ? extends Collection<V>> asMap()
      Description copied from interface: Multimap
      Return a map view with key to collection mapping. 返回键到集合映射的视图。
      Specified by:
      asMap in interface Multimap<K,V>
      Returns:
      map view | 映射视图
    • put

      public boolean put(K key, V value)
      Description copied from interface: Multimap
      Store a key-value pair. 存储键值对。
      Specified by:
      put in interface Multimap<K,V>
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      true if changed | 如果发生更改则返回 true
    • remove

      public boolean remove(Object key, Object value)
      Description copied from interface: Multimap
      Remove a single key-value pair. 移除单个键值对。
      Specified by:
      remove in interface Multimap<K,V>
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      true if removed | 如果移除则返回 true
    • putAll

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

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

      public Collection<V> replaceValues(K key, Iterable<? extends V> values)
      Description copied from interface: Multimap
      Replace all values for a key. 替换键的所有值。
      Specified by:
      replaceValues in interface Multimap<K,V>
      Parameters:
      key - the key | 键
      values - the new values | 新值
      Returns:
      previous values | 之前的值
    • removeAll

      public Collection<V> removeAll(Object key)
      Description copied from interface: Multimap
      Remove all values for a key. 移除键的所有值。
      Specified by:
      removeAll in interface Multimap<K,V>
      Parameters:
      key - the key | 键
      Returns:
      removed values | 移除的值
    • clear

      public void clear()
      Description copied from interface: Multimap
      Clear all entries. 清除所有条目。
      Specified by:
      clear in interface Multimap<K,V>