Class HashBiMap<K,V>

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

public class HashBiMap<K,V> extends Object implements BiMap<K,V>, Serializable
HashBiMap - Hash-based BiMap Implementation HashBiMap - 基于哈希的双向映射实现

A hash-based implementation of BiMap that maintains bidirectional mapping using two internal HashMaps.

基于哈希的 BiMap 实现,使用两个内部 HashMap 维护双向映射。

Features | 主要功能:

  • O(1) lookups in both directions - 双向 O(1) 查找
  • Unique keys and values enforced - 强制键和值唯一
  • Live inverse view - 实时反向视图
  • Serializable - 可序列化

Usage Examples | 使用示例:

// Create empty BiMap - 创建空 BiMap
HashBiMap<String, Integer> bimap = HashBiMap.create();

// Create with initial capacity - 创建指定容量
HashBiMap<String, Integer> bimap = HashBiMap.create(16);

// Create from existing map - 从现有映射创建
HashBiMap<String, Integer> bimap = HashBiMap.create(existingMap);

// Basic operations - 基本操作
bimap.put("a", 1);
bimap.get("a");        // 1
bimap.inverse().get(1); // "a"

Performance | 性能特性:

  • get: O(1) average - get: O(1) 平均
  • put: O(1) average - put: O(1) 平均
  • remove: O(1) average - remove: O(1) 平均
  • containsValue: O(1) - containsValue: O(1)

Security | 安全性:

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

    • create

      public static <K,V> HashBiMap<K,V> create()
      Create an empty HashBiMap. 创建空 HashBiMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new HashBiMap | 新的 HashBiMap
    • create

      public static <K,V> HashBiMap<K,V> create(int initialCapacity)
      Create an empty HashBiMap with initial capacity. 创建指定容量的空 HashBiMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      initialCapacity - initial capacity | 初始容量
      Returns:
      new HashBiMap | 新的 HashBiMap
    • create

      public static <K,V> HashBiMap<K,V> create(Map<? extends K, ? extends V> map)
      Create a HashBiMap from an existing map. 从现有映射创建 HashBiMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      map - source map | 源映射
      Returns:
      new HashBiMap | 新的 HashBiMap
      Throws:
      IllegalArgumentException - if duplicate values exist | 如果存在重复值
    • put

      public V put(K key, V value)
      Description copied from interface: BiMap
      Associates the specified value with the specified key. Throws exception if value already exists. 将指定值与指定键关联。如果值已存在则抛出异常。
      Specified by:
      put in interface BiMap<K,V>
      Specified by:
      put in interface Map<K,V>
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      the previous value, or null | 之前的值,或 null
    • forcePut

      public V forcePut(K key, V value)
      Description copied from interface: BiMap
      Force put: removes any existing entry with the same value before inserting. 强制放入:在插入前移除任何具有相同值的现有条目。
      Specified by:
      forcePut in interface BiMap<K,V>
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      the previous value for the key, or null | 该键之前的值,或 null
    • inverse

      public BiMap<V,K> inverse()
      Description copied from interface: BiMap
      Return the inverse view of this bimap. 返回此双向映射的反向视图。
      Specified by:
      inverse in interface BiMap<K,V>
      Returns:
      inverse bimap | 反向双向映射
    • size

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

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

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

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

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

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

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

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • keySet

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

      public Set<V> values()
      Description copied from interface: BiMap
      Return a set view of the values. 返回值的集合视图。
      Specified by:
      values in interface BiMap<K,V>
      Specified by:
      values in interface Map<K,V>
      Returns:
      value set | 值集合
    • entrySet

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

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

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

      public String toString()
      Overrides:
      toString in class Object