Interface BiMap<K,V>

Type Parameters:
K - key type | 键类型
V - value type | 值类型
All Superinterfaces:
Map<K,V>
All Known Implementing Classes:
HashBiMap

public interface BiMap<K,V> extends Map<K,V>
BiMap - Bidirectional Map Interface BiMap - 双向映射接口

A bidirectional map where both keys and values must be unique. Provides inverse view for value-to-key lookups.

双向映射,键和值都必须唯一。提供反向视图用于值到键的查找。

Features | 主要功能:

  • Bidirectional mapping - 双向映射
  • Unique keys and values - 唯一的键和值
  • Inverse view - 反向视图
  • Force put operation - 强制放入操作

Usage Examples | 使用示例:

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

// Put entries - 放入条目
bimap.put("one", 1);
bimap.put("two", 2);

// Get by key - 通过键获取
Integer value = bimap.get("one"); // 1

// Get inverse view - 获取反向视图
BiMap<Integer, String> inverse = bimap.inverse();
String key = inverse.get(1); // "one"

// Force put (replaces existing mapping) - 强制放入
bimap.forcePut("three", 1); // removes "one" -> 1

Performance | 性能特性:

  • get: O(1) for hash-based - get: O(1) 基于哈希
  • put: O(1) for hash-based - put: O(1) 基于哈希
  • inverse: O(1) - inverse: O(1)

Security | 安全性:

  • Thread-safe: Implementation dependent - 线程安全: 取决于实现
  • 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

    • put

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

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

      BiMap<V,K> inverse()
      Return the inverse view of this bimap. 返回此双向映射的反向视图。
      Returns:
      inverse bimap | 反向双向映射
    • values

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