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>
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for ImmutableBiMap ImmutableBiMap 构建器Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> ImmutableBiMap.Builder <K, V> builder()Return a new builder.voidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) static <K,V> ImmutableBiMap <K, V> Return an immutable bimap containing the entries of the given map.entrySet()booleanReturn the key mapped to the given value.inthashCode()inverse()Return the inverse view of this bimap.booleanisEmpty()keySet()static <K,V> ImmutableBiMap <K, V> of()Return an empty immutable bimap.static <K,V> ImmutableBiMap <K, V> of(K k1, V v1) Return an immutable bimap containing the given entry.static <K,V> ImmutableBiMap <K, V> of(K k1, V v1, K k2, V v2) Return an immutable bimap containing the given entries.voidintsize()toString()values()Methods inherited from class AbstractMap
cloneMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Method Details
-
of
Return an empty immutable bimap. 返回空不可变双向映射。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- empty immutable bimap | 空不可变双向映射
-
of
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
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
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
Return a new builder. 返回新构建器。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- builder | 构建器
-
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
-
entrySet
-
size
-
isEmpty
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V>
-
get
-
keySet
-
values
-
put
-
remove
-
putAll
-
clear
-
equals
-
hashCode
-
toString
- Overrides:
toStringin classAbstractMap<K,V>
-