Class ImmutableBiMap<K,V>

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

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

A bidirectional map that maintains a one-to-one correspondence between keys and values. Both keys and values must be unique. Cannot be modified after creation.

维护键和值之间一对一对应关系的双向映射。键和值都必须唯一。创建后不能修改。

Features | 主要功能:

  • Immutable - 不可变
  • Thread-safe - 线程安全
  • Null-safe (nulls not allowed) - 空值安全(不允许空值)
  • Bidirectional lookup - 双向查找
  • Unique keys and values - 唯一的键和值
  • O(1) lookup in both directions - O(1) 双向查找

Usage Examples | 使用示例:

// Create from entries - 从条目创建
ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.<String, Integer>builder()
    .put("one", 1)
    .put("two", 2)
    .put("three", 3)
    .build();

// Forward lookup - 正向查找
Integer value = bimap.get("one"); // Returns 1 - 返回 1

// Reverse lookup - 反向查找
ImmutableBiMap<Integer, String> inverse = bimap.inverse();
String key = inverse.get(1); // Returns "one" - 返回 "one"

// Create from map - 从映射创建
Map<String, Integer> map = Map.of("a", 1, "b", 2);
ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.copyOf(map);

Performance | 性能特性:

  • get: O(1) - get: O(1)
  • containsKey: O(1) - containsKey: O(1)
  • containsValue: O(1) - containsValue: O(1)
  • inverse: O(1) (cached) - inverse: 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

    • of

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

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

      public static <K,V> ImmutableBiMap<K,V> of(K k1, V v1, K k2, V v2)
      Return an immutable bimap 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 bimap | 不可变双向映射
    • copyOf

      public static <K,V> ImmutableBiMap<K,V> copyOf(Map<? extends K, ? extends V> map)
      Return an immutable bimap containing the entries of the given map. 返回包含给定映射条目的不可变双向映射。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      map - the map | 映射
      Returns:
      immutable bimap | 不可变双向映射
      Throws:
      OpenCollectionException - if duplicate values are found | 如果找到重复值
    • builder

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

      public ImmutableBiMap<V,K> inverse()
      Return the inverse view of this bimap. 返回此双向映射的反向视图。

      The inverse is cached and returns the same instance on repeated calls.

      反向视图被缓存,在重复调用时返回相同实例。

      Returns:
      the inverse bimap | 反向双向映射
    • getKey

      public K getKey(V value)
      Return the key mapped to the given value. 返回映射到给定值的键。
      Parameters:
      value - the value | 值
      Returns:
      the key, or null if not found | 键,如果未找到则返回 null
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • 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>
    • 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>
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class AbstractMap<K,V>
    • hashCode

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

      public String toString()
      Overrides:
      toString in class AbstractMap<K,V>