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>
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:
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) static <K,V> HashBiMap <K, V> create()Create an empty HashBiMap.static <K,V> HashBiMap <K, V> create(int initialCapacity) Create an empty HashBiMap with initial capacity.static <K,V> HashBiMap <K, V> Create a HashBiMap from an existing map.entrySet()booleanForce put: removes any existing entry with the same value before inserting.inthashCode()inverse()Return the inverse view of this bimap.booleanisEmpty()keySet()Associates the specified value with the specified key.voidintsize()toString()values()Return a set view of the values.Methods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Method Details
-
create
Create an empty HashBiMap. 创建空 HashBiMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new HashBiMap | 新的 HashBiMap
-
create
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
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
Description copied from interface:BiMapAssociates the specified value with the specified key. Throws exception if value already exists. 将指定值与指定键关联。如果值已存在则抛出异常。 -
forcePut
Description copied from interface:BiMapForce put: removes any existing entry with the same value before inserting. 强制放入:在插入前移除任何具有相同值的现有条目。 -
inverse
-
size
-
isEmpty
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V>
-
get
-
remove
-
putAll
-
clear
-
keySet
-
values
-
entrySet
-
equals
-
hashCode
-
toString
-