Class OpenMap
java.lang.Object
cloud.opencode.base.collections.OpenMap
OpenMap - Map Facade Utility Class
OpenMap - Map 门面工具类
Provides comprehensive map operations including creation, transformation, filtering, and querying.
提供全面的 Map 操作,包括创建、转换、过滤和查询。
Features | 主要功能:
- Factory methods for map creation - Map 创建工厂方法
- Map transformation - Map 转换
- Map filtering - Map 过滤
- Map difference computation - Map 差异计算
Usage Examples | 使用示例:
// Create map - 创建 Map
Map<String, Integer> map = OpenMap.of("a", 1, "b", 2);
// Filter by keys - 按键过滤
Map<String, Integer> filtered = OpenMap.filterKeys(map, k -> k.startsWith("a"));
// Transform values - 转换值
Map<String, String> transformed = OpenMap.transformValues(map, Object::toString);
Performance | 性能特性:
- Most operations: O(n) - 大多数操作: O(n)
- Single key operations: O(1) - 单键操作: O(1)
Security | 安全性:
- Thread-safe: No (use ConcurrentMap methods for thread safety) - 线程安全: 否
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K> booleancontainsAllKeys(Map<K, ?> map, K... keys) Check if map contains all keys.static <K,V> Map <K, V> filterEntries(Map<K, V> map, Predicate<? super Map.Entry<K, V>> predicate) Filter map by entries.static <K,V> Map <K, V> filterKeys(Map<K, V> map, Predicate<? super K> predicate) Filter map by keys.static <K,V> Map <K, V> filterValues(Map<K, V> map, Predicate<? super V> predicate) Filter map by values.static <K,V> HashMap <K, V> Create a HashMap from another map.static <K,V> V getOrDefault(Map<K, V> map, K key, V defaultValue) Get value safely with default.static <K,V> Map.Entry <K, V> immutableEntry(K key, V value) Create an immutable entry.static <K,V> ConcurrentMap <K, V> Create an empty ConcurrentHashMap.static <K,V> HashMap <K, V> Create an empty HashMap.static <K,V> IdentityHashMap <K, V> Create an empty IdentityHashMap.static <K,V> LinkedHashMap <K, V> Create an empty LinkedHashMap.static <K extends Comparable<K>, V>
TreeMap<K, V> Create an empty TreeMap.static <K,V> TreeMap <K, V> newTreeMap(Comparator<? super K> comparator) Create a TreeMap with comparator.static <K,V> HashMap <K, V> of(K k1, V v1) Create a HashMap with one entry.static <K,V> HashMap <K, V> of(K k1, V v1, K k2, V v2) Create a HashMap with two entries.static <K,V> HashMap <K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) Create a HashMap with three entries.static <K,V1, V2> Map <K, V2> transformEntries(Map<K, V1> map, EntryTransformer<? super K, ? super V1, V2> transformer) Transform entries in the map.static <K,V1, V2> Map <K, V2> transformValues(Map<K, V1> map, Function<? super V1, V2> function) Transform values in the map.static <K,V> Map <K, V> uniqueIndex(Iterable<V> values, Function<? super V, K> keyFunction) Create a map indexed by unique key.static <K,V> HashMap <K, V> withExpectedSize(int expectedSize) Create a HashMap with expected size.
-
Method Details
-
newHashMap
Create an empty HashMap. 创建空的 HashMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new HashMap | 新的 HashMap
-
of
Create a HashMap with one entry. 创建包含一个条目的 HashMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
k1- key | 键v1- value | 值- Returns:
- new HashMap | 新的 HashMap
-
of
Create a HashMap with two entries. 创建包含两个条目的 HashMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
k1- first key | 第一个键v1- first value | 第一个值k2- second key | 第二个键v2- second value | 第二个值- Returns:
- new HashMap | 新的 HashMap
-
of
Create a HashMap with three entries. 创建包含三个条目的 HashMap。- 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:
- new HashMap | 新的 HashMap
-
from
-
withExpectedSize
Create a HashMap with expected size. 创建具有预期大小的 HashMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
expectedSize- expected size | 预期大小- Returns:
- new HashMap | 新的 HashMap
-
newLinkedHashMap
Create an empty LinkedHashMap. 创建空的 LinkedHashMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new LinkedHashMap | 新的 LinkedHashMap
-
newTreeMap
Create an empty TreeMap. 创建空的 TreeMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new TreeMap | 新的 TreeMap
-
newTreeMap
Create a TreeMap with comparator. 创建具有比较器的 TreeMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
comparator- the comparator | 比较器- Returns:
- new TreeMap | 新的 TreeMap
-
newConcurrentMap
Create an empty ConcurrentHashMap. 创建空的 ConcurrentHashMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new ConcurrentHashMap | 新的 ConcurrentHashMap
-
newIdentityHashMap
Create an empty IdentityHashMap. 创建空的 IdentityHashMap。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new IdentityHashMap | 新的 IdentityHashMap
-
transformValues
Transform values in the map. 转换 Map 中的值。- Type Parameters:
K- key type | 键类型V1- input value type | 输入值类型V2- output value type | 输出值类型- Parameters:
map- the map | Mapfunction- the transform function | 转换函数- Returns:
- new map with transformed values | 包含转换后值的新 Map
-
transformEntries
public static <K,V1, Map<K,V2> V2> transformEntries(Map<K, V1> map, EntryTransformer<? super K, ? super V1, V2> transformer) Transform entries in the map. 转换 Map 中的条目。- Type Parameters:
K- key type | 键类型V1- input value type | 输入值类型V2- output value type | 输出值类型- Parameters:
map- the map | Maptransformer- the entry transformer | 条目转换器- Returns:
- new map with transformed entries | 包含转换后条目的新 Map
-
filterKeys
-
filterValues
-
filterEntries
-
uniqueIndex
Create a map indexed by unique key. 按唯一键创建索引 Map。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
values- the values | 值keyFunction- the key function | 键函数- Returns:
- indexed map | 索引 Map
- Throws:
IllegalArgumentException- if duplicate keys | 如果有重复键
-
getOrDefault
Get value safely with default. 安全获取值,带默认值。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
map- the map | Mapkey- the key | 键defaultValue- default value | 默认值- Returns:
- value or default | 值或默认值
-
containsAllKeys
Check if map contains all keys. 检查 Map 是否包含所有键。- Type Parameters:
K- key type | 键类型- Parameters:
map- the map | Mapkeys- the keys | 键- Returns:
- true if all keys present | 如果所有键都存在则返回 true
-
immutableEntry
Create an immutable entry. 创建不可变条目。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
key- the key | 键value- the value | 值- Returns:
- immutable entry | 不可变条目
-