Class OpenCollections
java.lang.Object
cloud.opencode.base.core.collect.OpenCollections
OpenCollections - Unmodifiable collection factory and utility methods.
OpenCollections - 不可修改集合的工厂与工具方法。
All methods return standard JDK unmodifiable collections (List,
Set, Map). Null elements and keys are rejected.
Every "mutation" method returns a new collection, leaving the
original unchanged.
所有方法返回标准 JDK 不可修改集合。拒绝 null 元素和键。 每个"变更"方法都返回新集合,原集合保持不变。
Thread Safety | 线程安全: All public methods are stateless and return unmodifiable collections, making them inherently thread-safe.
- Since:
- JDK 25, opencode-base-core V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA builder for creating unmodifiable lists incrementally.static final classA builder for creating unmodifiable maps incrementally. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T> Returns a new unmodifiable list with the given element appended.Splits a list into fixed-size chunks.static <T> List<T> Returns a new unmodifiable list that is the concatenation of two lists.static <T> Set<T> difference(Set<T> a, Set<T> b) Returns a new unmodifiable set containing elements inabut not inb.static <T,K> List <T> distinctBy(List<T> list, Function<? super T, ? extends K> keyExtractor) Returns a new list with duplicates removed based on a key extractor.static <T> List<T> Flattens a list of lists into a single unmodifiable list.frequencies(Collection<T> collection) Returns element frequencies as an unmodifiable map.Groups elements by a classifier function.static <T> Set<T> intersection(Set<T> a, Set<T> b) Returns a new unmodifiable set that is the intersection of two sets.static <T> OpenCollections.ListBuilder<T> Creates a newOpenCollections.ListBuilderwith default initial capacity.static <T> OpenCollections.ListBuilder<T> listBuilder(int expectedSize) Creates a newOpenCollections.ListBuilderwith the specified expected size.static <K,V> OpenCollections.MapBuilder <K, V> Creates a newOpenCollections.MapBuilderwith default initial capacity.Splits a list into two groups based on a predicate.static <T> List<T> Returns a new unmodifiable list with the given element prepended.Returns sliding windows of the given size with step 1.static <T> Set<T> Returns a new unmodifiable set that is the union of two sets.static <T> List<T> Returns a new unmodifiable list with the first occurrence of the given element removed.static <T> List<T> withReplaced(List<T> list, int index, T newElement) Returns a new unmodifiable list with the element at the given index replaced.Zips two lists into a list ofPairs, truncated to the shorter length.static <A,B, R> List <R> zipWith(List<A> a, List<B> b, BiFunction<? super A, ? super B, ? extends R> combiner) Zips two lists using a combiner function, truncated to the shorter length.
-
Method Details
-
listBuilder
Creates a newOpenCollections.ListBuilderwith default initial capacity. 创建具有默认初始容量的OpenCollections.ListBuilder。- Type Parameters:
T- the element type / 元素类型- Returns:
- a new list builder / 新列表构建器
-
listBuilder
Creates a newOpenCollections.ListBuilderwith the specified expected size. 创建具有指定预期大小的OpenCollections.ListBuilder。- Type Parameters:
T- the element type / 元素类型- Parameters:
expectedSize- the expected number of elements / 预期元素数量- Returns:
- a new list builder / 新列表构建器
- Throws:
IllegalArgumentException- if expectedSize is negative / 如果 expectedSize 为负数
-
mapBuilder
Creates a newOpenCollections.MapBuilderwith default initial capacity. 创建具有默认初始容量的OpenCollections.MapBuilder。- Type Parameters:
K- the key type / 键类型V- the value type / 值类型- Returns:
- a new map builder / 新映射构建器
-
append
Returns a new unmodifiable list with the given element appended. 返回在末尾追加给定元素的新不可修改列表。- Type Parameters:
T- the element type / 元素类型- Parameters:
list- the source list / 源列表element- the element to append (must not be null) / 要追加的元素(不可为 null)- Returns:
- a new unmodifiable list / 新不可修改列表
- Throws:
NullPointerException- if list or element is null / 如果 list 或 element 为 null
-
prepend
Returns a new unmodifiable list with the given element prepended. 返回在开头插入给定元素的新不可修改列表。- Type Parameters:
T- the element type / 元素类型- Parameters:
element- the element to prepend (must not be null) / 要插入的元素(不可为 null)list- the source list / 源列表- Returns:
- a new unmodifiable list / 新不可修改列表
- Throws:
NullPointerException- if list or element is null / 如果 list 或 element 为 null
-
concat
Returns a new unmodifiable list that is the concatenation of two lists. 返回两个列表连接后的新不可修改列表。- Type Parameters:
T- the element type / 元素类型- Parameters:
a- the first list / 第一个列表b- the second list / 第二个列表- Returns:
- a new unmodifiable list containing all elements of a followed by b / 包含 a 和 b 所有元素的新不可修改列表
- Throws:
NullPointerException- if a or b is null / 如果 a 或 b 为 null
-
without
Returns a new unmodifiable list with the first occurrence of the given element removed. 返回移除给定元素第一次出现后的新不可修改列表。If the element is not present, returns an unmodifiable copy of the original list.
如果元素不存在,返回原列表的不可修改副本。
- Type Parameters:
T- the element type / 元素类型- Parameters:
list- the source list / 源列表element- the element to remove (must not be null) / 要移除的元素(不可为 null)- Returns:
- a new unmodifiable list / 新不可修改列表
- Throws:
NullPointerException- if list or element is null / 如果 list 或 element 为 null
-
withReplaced
Returns a new unmodifiable list with the element at the given index replaced. 返回将指定索引处元素替换后的新不可修改列表。- Type Parameters:
T- the element type / 元素类型- Parameters:
list- the source list / 源列表index- the index of the element to replace / 要替换的元素索引newElement- the replacement element (must not be null) / 替换元素(不可为 null)- Returns:
- a new unmodifiable list / 新不可修改列表
- Throws:
NullPointerException- if list or newElement is null / 如果 list 或 newElement 为 nullIndexOutOfBoundsException- if index is out of range / 如果索引越界
-
union
Returns a new unmodifiable set that is the union of two sets. 返回两个集合并集的新不可修改集合。- Type Parameters:
T- the element type / 元素类型- Parameters:
a- the first set / 第一个集合b- the second set / 第二个集合- Returns:
- the union of a and b / a 和 b 的并集
- Throws:
NullPointerException- if a or b is null / 如果 a 或 b 为 null
-
intersection
Returns a new unmodifiable set that is the intersection of two sets. 返回两个集合交集的新不可修改集合。- Type Parameters:
T- the element type / 元素类型- Parameters:
a- the first set / 第一个集合b- the second set / 第二个集合- Returns:
- the intersection of a and b / a 和 b 的交集
- Throws:
NullPointerException- if a or b is null / 如果 a 或 b 为 null
-
difference
Returns a new unmodifiable set containing elements inabut not inb. 返回包含在a中但不在b中的元素的新不可修改集合。- Type Parameters:
T- the element type / 元素类型- Parameters:
a- the first set / 第一个集合b- the second set / 第二个集合- Returns:
- the difference a \ b / 差集 a \ b
- Throws:
NullPointerException- if a or b is null / 如果 a 或 b 为 null
-
partition
Splits a list into two groups based on a predicate. 根据谓词将列表分为两组。Elements matching the predicate are placed in the
truelist, others in thefalselist. Both result lists are unmodifiable.- Type Parameters:
T- the element type / 元素类型- Parameters:
list- the source list / 源列表predicate- the predicate to test elements / 测试元素的谓词- Returns:
- a map with
trueandfalsekeys / 包含 true 和 false 键的映射 - Throws:
NullPointerException- if list or predicate is null
-
groupBy
public static <T,K> Map<K,List<T>> groupBy(List<T> list, Function<? super T, ? extends K> classifier) Groups elements by a classifier function. 根据分类函数对元素进行分组。Returns an unmodifiable map with unmodifiable list values. Preserves insertion order for both keys and values.
- Type Parameters:
T- the element type / 元素类型K- the key type / 键类型- Parameters:
list- the source list / 源列表classifier- the classifier function / 分类函数- Returns:
- grouped elements / 分组后的元素
- Throws:
NullPointerException- if list, classifier, or any computed key is null
-
chunk
Splits a list into fixed-size chunks. The last chunk may be smaller. 将列表分成固定大小的块。最后一块可能较小。- Type Parameters:
T- the element type / 元素类型- Parameters:
list- the source list / 源列表size- the chunk size (must be positive) / 块大小(必须为正数)- Returns:
- unmodifiable list of unmodifiable chunks / 不可修改块列表
- Throws:
NullPointerException- if list is nullIllegalArgumentException- if size is not positive
-
sliding
Returns sliding windows of the given size with step 1. 返回步长为 1 的给定大小的滑动窗口。- Type Parameters:
T- the element type / 元素类型- Parameters:
list- the source list / 源列表size- the window size (must be positive) / 窗口大小(必须为正数)- Returns:
- unmodifiable list of unmodifiable windows / 不可修改窗口列表
- Throws:
NullPointerException- if list is nullIllegalArgumentException- if size is not positive
-
zip
- Type Parameters:
A- the first element type / 第一个元素类型B- the second element type / 第二个元素类型- Parameters:
a- the first list / 第一个列表b- the second list / 第二个列表- Returns:
- unmodifiable list of pairs / 不可修改的 Pair 列表
- Throws:
NullPointerException- if a or b is null
-
zipWith
public static <A,B, List<R> zipWithR> (List<A> a, List<B> b, BiFunction<? super A, ? super B, ? extends R> combiner) Zips two lists using a combiner function, truncated to the shorter length. 使用组合函数合并两个列表,截断到较短长度。- Type Parameters:
A- the first element type / 第一个元素类型B- the second element type / 第二个元素类型R- the result type / 结果类型- Parameters:
a- the first list / 第一个列表b- the second list / 第二个列表combiner- the combiner function / 组合函数- Returns:
- unmodifiable list of combined results / 不可修改的组合结果列表
- Throws:
NullPointerException- if any argument is null
-
distinctBy
Returns a new list with duplicates removed based on a key extractor. 根据键提取器去重,保留首次出现的顺序。- Type Parameters:
T- the element type / 元素类型K- the key type / 键类型- Parameters:
list- the source list / 源列表keyExtractor- the key extractor / 键提取器- Returns:
- unmodifiable deduplicated list / 不可修改的去重列表
- Throws:
NullPointerException- if list or keyExtractor is null
-
frequencies
Returns element frequencies as an unmodifiable map. 返回元素频率的不可修改映射。- Type Parameters:
T- the element type / 元素类型- Parameters:
collection- the source collection / 源集合- Returns:
- unmodifiable map of element to count / 不可修改的元素到计数映射
- Throws:
NullPointerException- if collection is null
-
flatten
Flattens a list of lists into a single unmodifiable list. 将嵌套列表展平为单个不可修改列表。- Type Parameters:
T- the element type / 元素类型- Parameters:
lists- the nested lists / 嵌套列表- Returns:
- unmodifiable flattened list / 不可修改的展平列表
- Throws:
NullPointerException- if lists is null
-
toUnmodifiableList
-
toUnmodifiableSet
-