Class MapUtil
java.lang.Object
cloud.opencode.base.collections.MapUtil
MapUtil - Map Utility Class
MapUtil - 映射工具类
Provides factory methods and operations for Map implementations including HashMap, LinkedHashMap, TreeMap, and ConcurrentMap.
提供 Map 实现的工厂方法和操作,包括 HashMap、LinkedHashMap、TreeMap 和 ConcurrentMap。
Features | 主要功能:
- Factory methods for map creation - Map 创建工厂方法
- Map difference calculation - Map 差异计算
- Transformation views - 转换视图
- Filtering views - 过滤视图
Usage Examples | 使用示例:
// Create HashMap - 创建 HashMap
Map<String, Integer> map = MapUtil.newHashMap();
// Unique index - 唯一索引
Map<String, User> byId = MapUtil.uniqueIndex(users, User::getId);
// Map difference - Map 差异
MapDifference<String, Integer> diff = MapUtil.difference(map1, map2);
// Transform values - 转换值
Map<String, String> stringValues = MapUtil.transformValues(map, Object::toString);
Performance | 性能特性:
- Factory methods: O(1) or O(n) - 工厂方法: O(1) 或 O(n)
- uniqueIndex: O(n) - uniqueIndex: O(n)
- difference: O(n + m) - difference: O(n + m)
Security | 安全性:
- Thread-safe: No (except ConcurrentMap) - 线程安全: 否(ConcurrentMap 除外)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceEntry transformer interface 条目转换器 -
Method Summary
Modifier and TypeMethodDescriptionstatic intcapacity(int expectedSize) Calculate capacity for expected size 计算 HashMap 所需容量static <K,V> MapDifference <K, V> difference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) Compute the difference between two maps 计算两个 Map 的差异static <K,V> MapDifference <K, V> difference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence) Calculate the difference between two maps using custom value equivalence.static <K,V> Map <K, V> filterEntries(Map<K, V> unfiltered, Predicate<? super Map.Entry<K, V>> entryPredicate) Return a view filtered by entries 过滤条目视图static <K,V> Map <K, V> filterKeys(Map<K, V> unfiltered, Predicate<? super K> keyPredicate) Return a view filtered by keys 过滤键视图static <K,V> Map <K, V> filterValues(Map<K, V> unfiltered, Predicate<? super V> valuePredicate) Return a view filtered by values 过滤值视图fromProperties(Properties properties) Create a Properties object to Map 从属性创建 Mapstatic <K,V> Map.Entry <K, V> immutableEntry(K key, V value) Create an immutable entry 创建不可变条目static <K,V> ConcurrentMap <K, V> Create a new ConcurrentMap 创建 ConcurrentMapstatic <K,V> HashMap <K, V> Create a new HashMap 创建 HashMapstatic <K,V> HashMap <K, V> newHashMap(Map<? extends K, ? extends V> map) Create a new HashMap from an existing Map 从 Map 创建 HashMapstatic <K,V> HashMap <K, V> newHashMapWithExpectedSize(int expectedSize) Create a new HashMap with expected size 创建指定容量的 HashMapstatic <K,V> IdentityHashMap <K, V> Create a new IdentityHashMap 创建 IdentityHashMapstatic <K,V> LinkedHashMap <K, V> Create a new LinkedHashMap 创建 LinkedHashMapstatic <K,V> LinkedHashMap <K, V> newLinkedHashMapWithExpectedSize(int expectedSize) Create a new LinkedHashMap with expected size 创建指定容量的 LinkedHashMapstatic <K extends Comparable<K>, V>
TreeMap<K, V> Create a new TreeMap 创建 TreeMapstatic <K,V> TreeMap <K, V> newTreeMap(Comparator<? super K> comparator) Create a new TreeMap with comparator 带比较器的 TreeMapstatic <K,V> BiMap <K, V> synchronizedBiMap(BiMap<K, V> bimap) Create a synchronized BiMap wrapper.static <K,V1, V2> Map <K, V2> transformEntries(Map<K, V1> fromMap, MapUtil.EntryTransformer<? super K, ? super V1, V2> transformer) Return a view with transformed entries 转换条目视图static <K,V1, V2> Map <K, V2> transformValues(Map<K, V1> fromMap, Function<? super V1, V2> function) Return a view with transformed values 转换值视图static <K,V> Map <K, V> uniqueIndex(Iterable<V> values, Function<? super V, K> keyFunction) Create a map indexed by unique keys extracted from values 按唯一键索引static <K,V> Map <K, V> uniqueIndex(Iterator<V> values, Function<? super V, K> keyFunction) Create a map indexed by unique keys extracted from values 按唯一键索引(Iterator)static <K,V> BiMap <K, V> unmodifiableBiMap(BiMap<? extends K, ? extends V> bimap) Create an unmodifiable BiMap wrapper.
-
Method Details
-
newHashMap
Create a new HashMap 创建 HashMap- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new HashMap | 新的 HashMap
-
newHashMap
-
newHashMapWithExpectedSize
Create a new HashMap with expected size 创建指定容量的 HashMap- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
expectedSize- expected size | 预期大小- Returns:
- new HashMap | 新的 HashMap
-
newLinkedHashMap
Create a new LinkedHashMap 创建 LinkedHashMap- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new LinkedHashMap | 新的 LinkedHashMap
-
newLinkedHashMapWithExpectedSize
Create a new LinkedHashMap with expected size 创建指定容量的 LinkedHashMap- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
expectedSize- expected size | 预期大小- Returns:
- new LinkedHashMap | 新的 LinkedHashMap
-
newTreeMap
Create a new TreeMap 创建 TreeMap- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new TreeMap | 新的 TreeMap
-
newTreeMap
Create a new TreeMap with comparator 带比较器的 TreeMap- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
comparator- the comparator | 比较器- Returns:
- new TreeMap | 新的 TreeMap
-
newConcurrentMap
Create a new ConcurrentMap 创建 ConcurrentMap- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new ConcurrentMap | 新的 ConcurrentMap
-
newIdentityHashMap
Create a new IdentityHashMap 创建 IdentityHashMap- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- new IdentityHashMap | 新的 IdentityHashMap
-
uniqueIndex
Create a map indexed by unique keys extracted from values 按唯一键索引- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
values- the values | 值keyFunction- the key extractor | 键提取器- Returns:
- indexed map | 索引后的 Map
- Throws:
OpenCollectionException- if duplicate keys | 如果有重复键
-
uniqueIndex
Create a map indexed by unique keys extracted from values 按唯一键索引(Iterator)- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
values- the values | 值keyFunction- the key extractor | 键提取器- Returns:
- indexed map | 索引后的 Map
- Throws:
OpenCollectionException- if duplicate keys | 如果有重复键
-
difference
public static <K,V> MapDifference<K,V> difference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) Compute the difference between two maps 计算两个 Map 的差异- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
left- left map | 左 Mapright- right map | 右 Map- Returns:
- map difference | Map 差异
-
transformValues
public static <K,V1, Map<K,V2> V2> transformValues(Map<K, V1> fromMap, Function<? super V1, V2> function) Return a view with transformed values 转换值视图- Type Parameters:
K- key type | 键类型V1- original value type | 原值类型V2- transformed value type | 转换后值类型- Parameters:
fromMap- source map | 源 Mapfunction- transform function | 转换函数- Returns:
- transformed view | 转换视图
-
transformEntries
public static <K,V1, Map<K,V2> V2> transformEntries(Map<K, V1> fromMap, MapUtil.EntryTransformer<? super K, ? super V1, V2> transformer) Return a view with transformed entries 转换条目视图- Type Parameters:
K- key type | 键类型V1- original value type | 原值类型V2- transformed value type | 转换后值类型- Parameters:
fromMap- source map | 源 Maptransformer- entry transformer | 条目转换器- Returns:
- transformed view | 转换视图
-
filterKeys
-
filterValues
Return a view filtered by values 过滤值视图- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
unfiltered- source map | 源 MapvaluePredicate- value predicate | 值谓词- Returns:
- filtered view | 过滤视图
-
filterEntries
public static <K,V> Map<K,V> filterEntries(Map<K, V> unfiltered, Predicate<? super Map.Entry<K, V>> entryPredicate) Return a view filtered by entries 过滤条目视图- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
unfiltered- source map | 源 MapentryPredicate- entry predicate | 条目谓词- Returns:
- filtered view | 过滤视图
-
fromProperties
Create a Properties object to Map 从属性创建 Map- Parameters:
properties- the properties | 属性- Returns:
- map | Map
-
immutableEntry
Create an immutable entry 创建不可变条目- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
key- the key | 键value- the value | 值- Returns:
- immutable entry | 不可变条目
-
capacity
public static int capacity(int expectedSize) Calculate capacity for expected size 计算 HashMap 所需容量- Parameters:
expectedSize- expected size | 预期大小- Returns:
- capacity | 容量
-
synchronizedBiMap
-
unmodifiableBiMap
-
difference
public static <K,V> MapDifference<K,V> difference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence) Calculate the difference between two maps using custom value equivalence. 使用自定义值等价关系计算两个 Map 的差异。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
left- left map | 左 Mapright- right map | 右 MapvalueEquivalence- value equivalence | 值等价关系- Returns:
- map difference | Map 差异
-