Class ImmutableCollectionUtil
java.lang.Object
cloud.opencode.base.collections.immutable.ImmutableCollectionUtil
ImmutableCollectionUtil - Utility Class for Immutable Collections
ImmutableCollectionUtil - 不可变集合工具类
Provides utility methods for working with immutable collections, including transformation, combination, and conversion operations.
提供用于处理不可变集合的工具方法,包括转换、组合和转换操作。
Features | 主要功能:
- Collection transformation - 集合转换
- Collection combination - 集合组合
- Type conversion - 类型转换
- Filtering and mapping - 过滤和映射
- Null-safe operations - 空值安全操作
Usage Examples | 使用示例:
// Transform list - 转换列表
ImmutableList<String> strings = ImmutableList.of("1", "2", "3");
ImmutableList<Integer> integers = ImmutableCollectionUtil.transform(strings, Integer::parseInt);
// Combine lists - 组合列表
ImmutableList<String> list1 = ImmutableList.of("a", "b");
ImmutableList<String> list2 = ImmutableList.of("c", "d");
ImmutableList<String> combined = ImmutableCollectionUtil.concat(list1, list2);
// Convert to multiset - 转换为多重集
ImmutableList<String> list = ImmutableList.of("a", "b", "a", "c");
ImmutableMultiset<String> multiset = ImmutableCollectionUtil.toMultiset(list);
// Create BiMap from map - 从映射创建双向映射
Map<String, Integer> map = Map.of("one", 1, "two", 2);
ImmutableBiMap<String, Integer> bimap = ImmutableCollectionUtil.toBiMap(map);
Performance | 性能特性:
- Most operations: O(n) where n is input size - 大多数操作: O(n),其中n是输入大小
- Null checks: O(1) - 空值检查: O(1)
- Type conversions: O(n) - 类型转换: O(n)
Security | 安全性:
- Thread-safe: Yes (all methods) - 线程安全: 是(所有方法)
- Null-safe: Yes (all methods) - 空值安全: 是(所有方法)
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E> ImmutableMultiset<E> combine(ImmutableMultiset<? extends E>... multisets) Combine multiple multisets.static <E> ImmutableList<E> concat(ImmutableList<? extends E>... lists) Concatenate multiple lists into one.static <E> ImmutableSet<E> difference(ImmutableSet<E> set1, ImmutableSet<E> set2) Difference of two sets (elements in set1 but not in set2).static <E> ImmutableList<E> filter(ImmutableList<E> list, Predicate<? super E> predicate) Filter a list based on a predicate.static <E> ImmutableSet<E> filter(ImmutableSet<E> set, Predicate<? super E> predicate) Filter a set based on a predicate.static <E> ImmutableSet<E> intersection(ImmutableSet<E> set1, ImmutableSet<E> set2) Intersection of two sets.static booleanisNullOrEmpty(ImmutableTable<?, ?, ?> table) Check if a table is null or empty.static booleanisNullOrEmpty(Collection<?> collection) Check if a collection is null or empty.static booleanisNullOrEmpty(Map<?, ?> map) Check if a map is null or empty.static <E> ImmutableList<E> reverse(ImmutableList<E> list) Reverse a list.static <E> ImmutableList<E> subList(ImmutableList<E> list, int fromIndex, int toIndex) Get a sublist.static <K,V> ImmutableBiMap <K, V> Convert a map to an immutable bimap.static <E> ImmutableList<E> toList(ImmutableMultiset<E> multiset) Convert an immutable multiset to an immutable list (preserving duplicates).static <E> ImmutableList<E> toList(ImmutableSet<E> set) Convert an immutable set to an immutable list.static <E> ImmutableMultiset<E> toMultiset(Collection<? extends E> collection) Convert a collection to an immutable multiset.static <E> ImmutableSet<E> toSet(ImmutableList<E> list) Convert an immutable list to an immutable set.static <R,C, V> ImmutableTable <R, C, V> Create a table from a nested map.static <T,R> ImmutableMultiset <R> transform(ImmutableMultiset<T> multiset, Function<? super T, ? extends R> function) Transform a multiset using a mapping function.static <T,R> ImmutableList <R> transform(ImmutableList<T> list, Function<? super T, ? extends R> function) Transform a list using a mapping function.static <T,R> ImmutableSet <R> transform(ImmutableSet<T> set, Function<? super T, ? extends R> function) Transform a set using a mapping function.static <R,C, V> ImmutableTable <C, R, V> transpose(ImmutableTable<R, C, V> table) Transpose a table (swap rows and columns).static <E> ImmutableSet<E> union(ImmutableSet<? extends E>... sets) Union of multiple sets.static <K,V> ImmutableBiMap <K, V> zipToBiMap(List<? extends K> keys, List<? extends V> values) Create a bimap from two lists (keys and values).
-
Method Details
-
transform
public static <T,R> ImmutableList<R> transform(ImmutableList<T> list, Function<? super T, ? extends R> function) Transform a list using a mapping function. 使用映射函数转换列表。- Type Parameters:
T- source element type | 源元素类型R- target element type | 目标元素类型- Parameters:
list- the list to transform | 要转换的列表function- the mapping function | 映射函数- Returns:
- transformed immutable list | 转换后的不可变列表
-
concat
Concatenate multiple lists into one. 将多个列表连接为一个。- Type Parameters:
E- element type | 元素类型- Parameters:
lists- the lists to concatenate | 要连接的列表- Returns:
- concatenated immutable list | 连接后的不可变列表
-
reverse
Reverse a list. 反转列表。- Type Parameters:
E- element type | 元素类型- Parameters:
list- the list to reverse | 要反转的列表- Returns:
- reversed immutable list | 反转后的不可变列表
-
subList
Get a sublist. 获取子列表。- Type Parameters:
E- element type | 元素类型- Parameters:
list- the list | 列表fromIndex- the start index (inclusive) | 起始索引(包含)toIndex- the end index (exclusive) | 结束索引(不包含)- Returns:
- sublist as immutable list | 作为不可变列表的子列表
-
transform
public static <T,R> ImmutableSet<R> transform(ImmutableSet<T> set, Function<? super T, ? extends R> function) Transform a set using a mapping function. 使用映射函数转换集合。- Type Parameters:
T- source element type | 源元素类型R- target element type | 目标元素类型- Parameters:
set- the set to transform | 要转换的集合function- the mapping function | 映射函数- Returns:
- transformed immutable set | 转换后的不可变集合
-
union
Union of multiple sets. 多个集合的并集。- Type Parameters:
E- element type | 元素类型- Parameters:
sets- the sets to union | 要合并的集合- Returns:
- union as immutable set | 作为不可变集合的并集
-
intersection
Intersection of two sets. 两个集合的交集。- Type Parameters:
E- element type | 元素类型- Parameters:
set1- the first set | 第一个集合set2- the second set | 第二个集合- Returns:
- intersection as immutable set | 作为不可变集合的交集
-
difference
Difference of two sets (elements in set1 but not in set2). 两个集合的差集(在set1中但不在set2中的元素)。- Type Parameters:
E- element type | 元素类型- Parameters:
set1- the first set | 第一个集合set2- the second set | 第二个集合- Returns:
- difference as immutable set | 作为不可变集合的差集
-
toMultiset
Convert a collection to an immutable multiset. 将集合转换为不可变多重集。- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合- Returns:
- immutable multiset | 不可变多重集
-
transform
public static <T,R> ImmutableMultiset<R> transform(ImmutableMultiset<T> multiset, Function<? super T, ? extends R> function) Transform a multiset using a mapping function. 使用映射函数转换多重集。- Type Parameters:
T- source element type | 源元素类型R- target element type | 目标元素类型- Parameters:
multiset- the multiset to transform | 要转换的多重集function- the mapping function | 映射函数- Returns:
- transformed immutable multiset | 转换后的不可变多重集
-
combine
@SafeVarargs public static <E> ImmutableMultiset<E> combine(ImmutableMultiset<? extends E>... multisets) Combine multiple multisets. 组合多个多重集。- Type Parameters:
E- element type | 元素类型- Parameters:
multisets- the multisets to combine | 要组合的多重集- Returns:
- combined immutable multiset | 组合后的不可变多重集
-
toBiMap
Convert a map to an immutable bimap. 将映射转换为不可变双向映射。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
map- the map | 映射- Returns:
- immutable bimap | 不可变双向映射
- Throws:
OpenCollectionException- if duplicate values are found | 如果找到重复值
-
zipToBiMap
public static <K,V> ImmutableBiMap<K,V> zipToBiMap(List<? extends K> keys, List<? extends V> values) Create a bimap from two lists (keys and values). 从两个列表(键和值)创建双向映射。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
keys- the keys | 键values- the values | 值- Returns:
- immutable bimap | 不可变双向映射
- Throws:
OpenCollectionException- if lists have different sizes or duplicate keys/values | 如果列表大小不同或有重复的键/值
-
toTable
public static <R,C, ImmutableTable<R,V> C, toTableV> (Map<? extends R, ? extends Map<? extends C, ? extends V>> nestedMap) Create a table from a nested map. 从嵌套映射创建表格。- Type Parameters:
R- row key type | 行键类型C- column key type | 列键类型V- value type | 值类型- Parameters:
nestedMap- the nested map (row -> column -> value) | 嵌套映射(行 -> 列 -> 值)- Returns:
- immutable table | 不可变表格
-
transpose
Transpose a table (swap rows and columns). 转置表格(交换行和列)。- Type Parameters:
R- row key type | 行键类型C- column key type | 列键类型V- value type | 值类型- Parameters:
table- the table to transpose | 要转置的表格- Returns:
- transposed immutable table | 转置后的不可变表格
-
toSet
Convert an immutable list to an immutable set. 将不可变列表转换为不可变集合。- Type Parameters:
E- element type | 元素类型- Parameters:
list- the list | 列表- Returns:
- immutable set | 不可变集合
-
toList
Convert an immutable set to an immutable list. 将不可变集合转换为不可变列表。- Type Parameters:
E- element type | 元素类型- Parameters:
set- the set | 集合- Returns:
- immutable list | 不可变列表
-
toList
Convert an immutable multiset to an immutable list (preserving duplicates). 将不可变多重集转换为不可变列表(保留重复项)。- Type Parameters:
E- element type | 元素类型- Parameters:
multiset- the multiset | 多重集- Returns:
- immutable list with duplicates | 带重复项的不可变列表
-
filter
Filter a list based on a predicate. 基于谓词过滤列表。- Type Parameters:
E- element type | 元素类型- Parameters:
list- the list to filter | 要过滤的列表predicate- the filter predicate | 过滤谓词- Returns:
- filtered immutable list | 过滤后的不可变列表
-
filter
Filter a set based on a predicate. 基于谓词过滤集合。- Type Parameters:
E- element type | 元素类型- Parameters:
set- the set to filter | 要过滤的集合predicate- the filter predicate | 过滤谓词- Returns:
- filtered immutable set | 过滤后的不可变集合
-
isNullOrEmpty
Check if a collection is null or empty. 检查集合是否为空或为null。- Parameters:
collection- the collection | 集合- Returns:
- true if null or empty | 如果为null或为空则返回true
-
isNullOrEmpty
Check if a map is null or empty. 检查映射是否为空或为null。- Parameters:
map- the map | 映射- Returns:
- true if null or empty | 如果为null或为空则返回true
-
isNullOrEmpty
Check if a table is null or empty. 检查表格是否为空或为null。- Parameters:
table- the table | 表格- Returns:
- true if null or empty | 如果为null或为空则返回true
-