Class ImmutableMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
cloud.opencode.base.collections.ImmutableMap<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型
All Implemented Interfaces:
Serializable, Map<K,V>

public final class ImmutableMap<K,V> extends AbstractMap<K,V> implements Serializable
ImmutableMap - Immutable Map Implementation ImmutableMap - 不可变映射实现

A map that cannot be modified after creation. Any attempt to modify the map will throw an exception.

创建后不能修改的映射。任何修改映射的尝试都会抛出异常。

Features | 主要功能:

  • Immutable - 不可变
  • Thread-safe - 线程安全
  • Null-safe (nulls not allowed) - 空值安全(不允许空值)
  • O(1) contains check - O(1) 包含检查

Usage Examples | 使用示例:

// Create from key-value pairs - 从键值对创建
ImmutableMap<String, Integer> map = ImmutableMap.of("a", 1, "b", 2);

// Create from map - 从映射创建
ImmutableMap<String, Integer> map = ImmutableMap.copyOf(existingMap);

// Use builder - 使用构建器
ImmutableMap<String, Integer> map = ImmutableMap.<String, Integer>builder()
    .put("a", 1)
    .put("b", 2)
    .build();

Performance | 性能特性:

  • get: O(1) average - get: O(1) 平均
  • containsKey: O(1) average - containsKey: O(1) 平均
  • containsValue: O(n) - containsValue: O(n)

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

    • of

      public static <K,V> ImmutableMap<K,V> of()
      Return an empty immutable map. 返回空不可变映射。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      empty immutable map | 空不可变映射
    • of

      public static <K,V> ImmutableMap<K,V> of(K k1, V v1)
      Return an immutable map containing the given entry. 返回包含给定条目的不可变映射。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      k1 - the key | 键
      v1 - the value | 值
      Returns:
      immutable map | 不可变映射
    • of

      public static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2)
      Return an immutable map containing the given entries. 返回包含给定条目的不可变映射。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      k1 - first key | 第一个键
      v1 - first value | 第一个值
      k2 - second key | 第二个键
      v2 - second value | 第二个值
      Returns:
      immutable map | 不可变映射
    • of

      public static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
      Return an immutable map containing the given entries. 返回包含给定条目的不可变映射。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      k1 - first key | 第一个键
      v1 - first value | 第一个值
      k2 - second key | 第二个键
      v2 - second value | 第二个值
      k3 - third key | 第三个键
      v3 - third value | 第三个值
      Returns:
      immutable map | 不可变映射
    • of

      public static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
      Return an immutable map containing the given entries. 返回包含给定条目的不可变映射。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      k1 - first key | 第一个键
      v1 - first value | 第一个值
      k2 - second key | 第二个键
      v2 - second value | 第二个值
      k3 - third key | 第三个键
      v3 - third value | 第三个值
      k4 - fourth key | 第四个键
      v4 - fourth value | 第四个值
      Returns:
      immutable map | 不可变映射
    • of

      public static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
      Return an immutable map containing the given entries. 返回包含给定条目的不可变映射。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      k1 - first key | 第一个键
      v1 - first value | 第一个值
      k2 - second key | 第二个键
      v2 - second value | 第二个值
      k3 - third key | 第三个键
      v3 - third value | 第三个值
      k4 - fourth key | 第四个键
      v4 - fourth value | 第四个值
      k5 - fifth key | 第五个键
      v5 - fifth value | 第五个值
      Returns:
      immutable map | 不可变映射
    • copyOf

      public static <K,V> ImmutableMap<K,V> copyOf(Map<? extends K, ? extends V> map)
      Return an immutable map containing the elements of the given map. 返回包含给定映射元素的不可变映射。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      map - the map | 映射
      Returns:
      immutable map | 不可变映射
    • builder

      public static <K,V> ImmutableMap.Builder<K,V> builder()
      Return a new builder. 返回新构建器。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      builder | 构建器
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
      Overrides:
      isEmpty in class AbstractMap<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
      Overrides:
      keySet in class AbstractMap<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
      Overrides:
      values in class AbstractMap<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
    • putAll

      public void putAll(Map<? extends K, ? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class AbstractMap<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface Map<K,V>
    • remove

      public boolean remove(Object key, Object value)
      Specified by:
      remove in interface Map<K,V>
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      Specified by:
      replace in interface Map<K,V>
    • replace

      public V replace(K key, V value)
      Specified by:
      replace in interface Map<K,V>
    • computeIfAbsent

      public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
      Specified by:
      computeIfAbsent in interface Map<K,V>
    • computeIfPresent

      public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
      Specified by:
      computeIfPresent in interface Map<K,V>
    • compute

      public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
      Specified by:
      compute in interface Map<K,V>
    • merge

      public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
      Specified by:
      merge in interface Map<K,V>
    • replaceAll

      public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
      Specified by:
      replaceAll in interface Map<K,V>