Class OpenCollection
java.lang.Object
cloud.opencode.base.collections.OpenCollection
OpenCollection - General Collection Utility Class
OpenCollection - 通用集合工具类
Provides comprehensive collection operations including null-safe checks, set operations, filtering, transformations, and batch operations.
提供全面的集合操作,包括空值安全检查、集合运算、过滤、转换和批量操作。
Features | 主要功能:
- Null-safe operations - 空值安全操作
- Set algebra (union, intersection, difference) - 集合代数(并集、交集、差集)
- Element statistics and counting - 元素统计和计数
- Filtering and transformation - 过滤和转换
- Batch operations - 批量操作
Usage Examples | 使用示例:
// Check if empty - 检查是否为空
boolean empty = OpenCollection.isEmpty(list);
// Union of collections - 集合并集
Collection<String> union = OpenCollection.union(list1, list2);
// Filter elements - 过滤元素
Collection<String> filtered = OpenCollection.select(list, s -> s.startsWith("A"));
// Count matches - 统计匹配数
int count = OpenCollection.countMatches(list, s -> s.length() > 5);
Performance | 性能特性:
- Most operations: O(n) - 大多数操作: O(n)
- Set operations: O(n + m) - 集合运算: O(n + m)
Security | 安全性:
- Thread-safe: No (operations on mutable collections are not synchronized) - 线程安全: 否
- 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 <E> booleanaddAll(Collection<E> collection, E... elements) Add all array elements to collection 批量添加数组元素static <E> booleanaddAll(Collection<E> collection, Iterable<? extends E> iterable) Add all iterable elements to collection 批量添加 Iterable 元素static <E> booleanaddAll(Collection<E> collection, Iterator<? extends E> iterator) Add all iterator elements to collection 批量添加 Iterator 元素static <E> booleanaddIgnoreNull(Collection<E> collection, E object) Add element to collection if not null 如果元素不为 null 则添加static intcardinality(Object obj, Iterable<?> coll) Count the occurrences of an object in an iterable 统计元素出现次数static <E> List<E> collate(Iterable<? extends E> a, Iterable<? extends E> b, Comparator<? super E> c) Merge two sorted iterables 合并两个有序集合static <I,O> Collection <O> Collect transformed elements into a new collection 收集转换结果到新集合static booleancontainsAll(Collection<?> coll, Collection<?> elements) Check if collection contains all the given elements 检查是否包含所有元素static booleancontainsAny(Collection<?> coll, Object... elements) Check if collection contains any of the given elements 检查是否包含任一元素static <E> intcountMatches(Iterable<E> input, Predicate<? super E> predicate) Count elements matching the predicate 统计满足条件的元素数量static <E> Collection<E> disjunction(Iterable<? extends E> a, Iterable<? extends E> b) Returns the symmetric difference of two iterables (a XOR b) 返回两个可迭代对象的对称差集 (a XOR b)static <E> Collection<E> emptyIfNull(Collection<E> collection) Return an empty collection if the input is null 空集合返回默认值static <E> booleanCheck if any element matches the predicate 检查是否存在匹配元素static <E> EextractSingleton(Collection<E> collection) Extract the single element from a collection 从单元素集合提取元素static <E> booleanFilter collection in-place (retain matching elements) 原地过滤(保留匹配元素)static <E> booleanfilterInverse(Iterable<E> collection, Predicate<? super E> predicate) Filter collection in-place (remove matching elements) 原地反向过滤(移除匹配元素)static <E> EFind the first matching element 查找第一个匹配元素static <E> EGet the n-th element from an iterable 安全获取第 N 个元素getCardinalityMap(Iterable<? extends E> coll) Get the cardinality map of an iterable 获取元素基数映射static <E> EGet the first element from an iterable 获取第一个元素static <E> EGet the last element from an iterable 获取最后一个元素static <E> Collection<E> intersection(Iterable<? extends E> a, Iterable<? extends E> b) Returns the intersection of two iterables 返回两个可迭代对象的交集static booleanCheck if iterable is empty 判断 Iterable 是否为空static booleanisEmpty(Collection<?> coll) Check if collection is empty (null or empty) 判断集合是否为空(null 或 empty)static booleanCheck if map is empty (null or empty) 判断 Map 是否为空static booleanisEqualCollection(Collection<?> a, Collection<?> b) Compare two collections for equality (ignoring order) 比较两个集合是否相等(忽略顺序)static booleanisNotEmpty(Collection<?> coll) Check if collection is not empty 判断集合是否非空static booleanisNotEmpty(Map<?, ?> map) Check if map is not empty 判断 Map 是否非空static booleanisProperSubCollection(Collection<?> sub, Collection<?> sup) Check if sub is a proper subset of sup 检查是否为真子集static booleanisSubCollection(Collection<?> sub, Collection<?> sup) Check if sub is a subset of sup 检查是否为子集static <E> booleanmatchesAll(Iterable<E> input, Predicate<? super E> predicate) Check if all elements match the predicate 检查所有元素是否匹配static <E> Collection<List<E>> permutations(Collection<E> collection) Generate all permutations of a collection 生成所有排列static <E> booleanremoveNulls(Collection<E> collection) Remove all null elements from collection 移除所有 null 元素static voidreverseArray(Object[] array) Reverse an array in-place 反转数组static <E> Collection<E> Select matching elements into a new collection 选择匹配元素到新集合static <E> Collection<E> selectRejected(Iterable<? extends E> input, Predicate<? super E> predicate) Select non-matching elements into a new collection 选择不匹配元素到新集合static intGet the size of an object (collection, map, array, or iterable) 安全获取集合大小static <E> Collection<E> Returns the difference of two iterables (a - b) 返回两个可迭代对象的差集 (a - b)static <E> voidtransform(Collection<E> collection, UnaryOperator<E> transformer) Transform collection in-place 原地转换static <E> Collection<E> Returns the union of two iterables 返回两个可迭代对象的并集
-
Method Details
-
isEmpty
Check if collection is empty (null or empty) 判断集合是否为空(null 或 empty)- Parameters:
coll- the collection to check | 要检查的集合- Returns:
- true if null or empty | 如果为 null 或空则返回 true
-
isNotEmpty
Check if collection is not empty 判断集合是否非空- Parameters:
coll- the collection to check | 要检查的集合- Returns:
- true if not null and not empty | 如果非 null 且非空则返回 true
-
isEmpty
Check if map is empty (null or empty) 判断 Map 是否为空- Parameters:
map- the map to check | 要检查的 Map- Returns:
- true if null or empty | 如果为 null 或空则返回 true
-
isNotEmpty
Check if map is not empty 判断 Map 是否非空- Parameters:
map- the map to check | 要检查的 Map- Returns:
- true if not null and not empty | 如果非 null 且非空则返回 true
-
isEmpty
Check if iterable is empty 判断 Iterable 是否为空- Parameters:
iterable- the iterable to check | 要检查的 Iterable- Returns:
- true if null or empty | 如果为 null 或空则返回 true
-
union
Returns the union of two iterables 返回两个可迭代对象的并集- Type Parameters:
E- element type | 元素类型- Parameters:
a- first iterable | 第一个可迭代对象b- second iterable | 第二个可迭代对象- Returns:
- union collection | 并集
-
intersection
Returns the intersection of two iterables 返回两个可迭代对象的交集- Type Parameters:
E- element type | 元素类型- Parameters:
a- first iterable | 第一个可迭代对象b- second iterable | 第二个可迭代对象- Returns:
- intersection collection | 交集
-
subtract
Returns the difference of two iterables (a - b) 返回两个可迭代对象的差集 (a - b)- Type Parameters:
E- element type | 元素类型- Parameters:
a- first iterable | 第一个可迭代对象b- second iterable | 第二个可迭代对象- Returns:
- difference collection | 差集
-
disjunction
Returns the symmetric difference of two iterables (a XOR b) 返回两个可迭代对象的对称差集 (a XOR b)- Type Parameters:
E- element type | 元素类型- Parameters:
a- first iterable | 第一个可迭代对象b- second iterable | 第二个可迭代对象- Returns:
- symmetric difference collection | 对称差集
-
containsAny
Check if collection contains any of the given elements 检查是否包含任一元素- Parameters:
coll- the collection to check | 要检查的集合elements- the elements to look for | 要查找的元素- Returns:
- true if any element is found | 如果找到任一元素则返回 true
-
containsAll
Check if collection contains all the given elements 检查是否包含所有元素- Parameters:
coll- the collection to check | 要检查的集合elements- the elements to look for | 要查找的元素集合- Returns:
- true if all elements are found | 如果找到所有元素则返回 true
-
isEqualCollection
Compare two collections for equality (ignoring order) 比较两个集合是否相等(忽略顺序)- Parameters:
a- first collection | 第一个集合b- second collection | 第二个集合- Returns:
- true if equal | 如果相等则返回 true
-
isSubCollection
Check if sub is a subset of sup 检查是否为子集- Parameters:
sub- potential subset | 潜在子集sup- potential superset | 潜在超集- Returns:
- true if sub is a subset of sup | 如果 sub 是 sup 的子集则返回 true
-
isProperSubCollection
Check if sub is a proper subset of sup 检查是否为真子集- Parameters:
sub- potential proper subset | 潜在真子集sup- potential superset | 潜在超集- Returns:
- true if sub is a proper subset of sup | 如果 sub 是 sup 的真子集则返回 true
-
getCardinalityMap
-
cardinality
-
countMatches
-
filter
Filter collection in-place (retain matching elements) 原地过滤(保留匹配元素)- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合predicate- the predicate | 谓词- Returns:
- true if collection was modified | 如果集合被修改则返回 true
-
filterInverse
Filter collection in-place (remove matching elements) 原地反向过滤(移除匹配元素)- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合predicate- the predicate | 谓词- Returns:
- true if collection was modified | 如果集合被修改则返回 true
-
select
Select matching elements into a new collection 选择匹配元素到新集合- Type Parameters:
E- element type | 元素类型- Parameters:
input- the input iterable | 输入可迭代对象predicate- the predicate | 谓词- Returns:
- collection of matching elements | 匹配元素集合
-
selectRejected
public static <E> Collection<E> selectRejected(Iterable<? extends E> input, Predicate<? super E> predicate) Select non-matching elements into a new collection 选择不匹配元素到新集合- Type Parameters:
E- element type | 元素类型- Parameters:
input- the input iterable | 输入可迭代对象predicate- the predicate | 谓词- Returns:
- collection of non-matching elements | 不匹配元素集合
-
transform
Transform collection in-place 原地转换- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合transformer- the transformer | 转换器
-
collect
public static <I,O> Collection<O> collect(Iterable<I> input, Function<? super I, ? extends O> transformer) Collect transformed elements into a new collection 收集转换结果到新集合- Type Parameters:
I- input element type | 输入元素类型O- output element type | 输出元素类型- Parameters:
input- the input iterable | 输入可迭代对象transformer- the transformer | 转换器- Returns:
- collection of transformed elements | 转换后的元素集合
-
find
-
exists
Check if any element matches the predicate 检查是否存在匹配元素- Type Parameters:
E- element type | 元素类型- Parameters:
input- the iterable | 可迭代对象predicate- the predicate | 谓词- Returns:
- true if any element matches | 如果有任何元素匹配则返回 true
-
matchesAll
Check if all elements match the predicate 检查所有元素是否匹配- Type Parameters:
E- element type | 元素类型- Parameters:
input- the iterable | 可迭代对象predicate- the predicate | 谓词- Returns:
- true if all elements match | 如果所有元素都匹配则返回 true
-
extractSingleton
Extract the single element from a collection 从单元素集合提取元素- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合- Returns:
- the single element | 单个元素
- Throws:
OpenCollectionException- if collection size is not 1 | 如果集合大小不为 1
-
addAll
Add all array elements to collection 批量添加数组元素- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合elements- the elements to add | 要添加的元素- Returns:
- true if collection was modified | 如果集合被修改则返回 true
-
addAll
Add all iterable elements to collection 批量添加 Iterable 元素- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合iterable- the elements to add | 要添加的元素- Returns:
- true if collection was modified | 如果集合被修改则返回 true
-
addAll
Add all iterator elements to collection 批量添加 Iterator 元素- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合iterator- the elements to add | 要添加的元素- Returns:
- true if collection was modified | 如果集合被修改则返回 true
-
addIgnoreNull
Add element to collection if not null 如果元素不为 null 则添加- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合object- the element to add | 要添加的元素- Returns:
- true if element was added | 如果元素被添加则返回 true
-
removeNulls
Remove all null elements from collection 移除所有 null 元素- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合- Returns:
- true if collection was modified | 如果集合被修改则返回 true
-
permutations
Generate all permutations of a collection 生成所有排列- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合- Returns:
- all permutations | 所有排列
-
collate
public static <E> List<E> collate(Iterable<? extends E> a, Iterable<? extends E> b, Comparator<? super E> c) Merge two sorted iterables 合并两个有序集合- Type Parameters:
E- element type | 元素类型- Parameters:
a- first iterable | 第一个可迭代对象b- second iterable | 第二个可迭代对象c- comparator | 比较器- Returns:
- merged sorted list | 合并后的有序列表
-
reverseArray
Reverse an array in-place 反转数组- Parameters:
array- the array to reverse | 要反转的数组
-
size
Get the size of an object (collection, map, array, or iterable) 安全获取集合大小- Parameters:
object- the object | 对象- Returns:
- size or 0 if null | 大小或 0
-
get
Get the n-th element from an iterable 安全获取第 N 个元素- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象index- the index | 索引- Returns:
- the element at index | 索引处的元素
- Throws:
IndexOutOfBoundsException- if index is out of bounds | 如果索引越界
-
getFirst
Get the first element from an iterable 获取第一个元素- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象- Returns:
- the first element or null | 第一个元素或 null
-
getLast
Get the last element from an iterable 获取最后一个元素- Type Parameters:
E- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象- Returns:
- the last element or null | 最后一个元素或 null
-
emptyIfNull
Return an empty collection if the input is null 空集合返回默认值- Type Parameters:
E- element type | 元素类型- Parameters:
collection- the collection | 集合- Returns:
- collection or empty list | 集合或空列表
-