Class ImmutableMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
cloud.opencode.base.collections.ImmutableMap<K,V>
- Type Parameters:
K- key type | 键类型V- value type | 值类型
- All Implemented Interfaces:
Serializable, Map<K,V>
ImmutableMap - Immutable Map Implementation
ImmutableMap - 不可变映射实现
A map that cannot be modified after creation. Any attempt to modify the map will throw an exception.
创建后不能修改的映射。任何修改映射的尝试都会抛出异常。
Features | 主要功能:
- Immutable - 不可变
- Thread-safe - 线程安全
- Null-safe (nulls not allowed) - 空值安全(不允许空值)
- O(1) contains check - O(1) 包含检查
Usage Examples | 使用示例:
// Create from key-value pairs - 从键值对创建
ImmutableMap<String, Integer> map = ImmutableMap.of("a", 1, "b", 2);
// Create from map - 从映射创建
ImmutableMap<String, Integer> map = ImmutableMap.copyOf(existingMap);
// Use builder - 使用构建器
ImmutableMap<String, Integer> map = ImmutableMap.<String, Integer>builder()
.put("a", 1)
.put("b", 2)
.build();
Performance | 性能特性:
- get: O(1) average - get: O(1) 平均
- containsKey: O(1) average - containsKey: O(1) 平均
- containsValue: O(n) - containsValue: O(n)
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 ImmutableMap ImmutableMap 构建器Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> ImmutableMap.Builder <K, V> builder()Return a new builder.voidclear()computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) booleancontainsKey(Object key) booleancontainsValue(Object value) static <K,V> ImmutableMap <K, V> Return an immutable map containing the elements of the given map.entrySet()booleanisEmpty()keySet()static <K,V> ImmutableMap <K, V> of()Return an empty immutable map.static <K,V> ImmutableMap <K, V> of(K k1, V v1) Return an immutable map containing the given entry.static <K,V> ImmutableMap <K, V> of(K k1, V v1, K k2, V v2) Return an immutable map containing the given entries.static <K,V> ImmutableMap <K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) Return an immutable map containing the given entries.static <K,V> ImmutableMap <K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) Return an immutable map containing the given entries.static <K,V> ImmutableMap <K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) Return an immutable map containing the given entries.voidputIfAbsent(K key, V value) booleanbooleanvoidreplaceAll(BiFunction<? super K, ? super V, ? extends V> function) intsize()values()Methods inherited from class AbstractMap
clone, equals, hashCode, toStringMethods inherited from interface Map
forEach, getOrDefault
-
Method Details
-
of
Return an empty immutable map. 返回空不可变映射。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- empty immutable map | 空不可变映射
-
of
Return an immutable map containing the given entry. 返回包含给定条目的不可变映射。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
k1- the key | 键v1- the value | 值- Returns:
- immutable map | 不可变映射
-
of
Return an immutable map 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 map | 不可变映射
-
of
Return an immutable map 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 | 第二个值k3- third key | 第三个键v3- third value | 第三个值- Returns:
- immutable map | 不可变映射
-
of
Return an immutable map 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 | 第二个值k3- third key | 第三个键v3- third value | 第三个值k4- fourth key | 第四个键v4- fourth value | 第四个值- Returns:
- immutable map | 不可变映射
-
of
public static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) Return an immutable map 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 | 第二个值k3- third key | 第三个键v3- third value | 第三个值k4- fourth key | 第四个键v4- fourth value | 第四个值k5- fifth key | 第五个键v5- fifth value | 第五个值- Returns:
- immutable map | 不可变映射
-
copyOf
Return an immutable map containing the elements of the given map. 返回包含给定映射元素的不可变映射。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
map- the map | 映射- Returns:
- immutable map | 不可变映射
-
builder
Return a new builder. 返回新构建器。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- builder | 构建器
-
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
-
entrySet
-
put
-
remove
-
putAll
-
clear
-
putIfAbsent
-
remove
-
replace
-
replace
-
computeIfAbsent
-
computeIfPresent
- Specified by:
computeIfPresentin interfaceMap<K,V>
-
compute
-
merge
-
replaceAll
- Specified by:
replaceAllin interfaceMap<K,V>
-