Class OpenCollections

java.lang.Object
cloud.opencode.base.core.collect.OpenCollections

public final class OpenCollections extends Object
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
  • Method Details

    • listBuilder

      public static <T> OpenCollections.ListBuilder<T> listBuilder()
      Creates a new OpenCollections.ListBuilder with default initial capacity. 创建具有默认初始容量的 OpenCollections.ListBuilder
      Type Parameters:
      T - the element type / 元素类型
      Returns:
      a new list builder / 新列表构建器
    • listBuilder

      public static <T> OpenCollections.ListBuilder<T> listBuilder(int expectedSize)
      Creates a new OpenCollections.ListBuilder with 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

      public static <K,V> OpenCollections.MapBuilder<K,V> mapBuilder()
      Creates a new OpenCollections.MapBuilder with default initial capacity. 创建具有默认初始容量的 OpenCollections.MapBuilder
      Type Parameters:
      K - the key type / 键类型
      V - the value type / 值类型
      Returns:
      a new map builder / 新映射构建器
    • append

      public static <T> List<T> append(List<T> list, T element)
      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

      public static <T> List<T> prepend(T element, List<T> list)
      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

      public static <T> List<T> concat(List<T> a, List<T> b)
      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

      public static <T> List<T> without(List<T> list, T element)
      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

      public 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. 返回将指定索引处元素替换后的新不可修改列表。
      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 为 null
      IndexOutOfBoundsException - if index is out of range / 如果索引越界
    • union

      public static <T> Set<T> union(Set<T> a, Set<T> b)
      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

      public static <T> Set<T> intersection(Set<T> a, Set<T> b)
      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

      public static <T> Set<T> difference(Set<T> a, Set<T> b)
      Returns a new unmodifiable set containing elements in a but not in b. 返回包含在 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

      public static <T> Map<Boolean,List<T>> partition(List<T> list, Predicate<? super T> predicate)
      Splits a list into two groups based on a predicate. 根据谓词将列表分为两组。

      Elements matching the predicate are placed in the true list, others in the false list. 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 true and false keys / 包含 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

      public static <T> List<List<T>> chunk(List<T> list, int size)
      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 null
      IllegalArgumentException - if size is not positive
    • sliding

      public static <T> List<List<T>> sliding(List<T> list, int size)
      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 null
      IllegalArgumentException - if size is not positive
    • zip

      public static <A,B> List<Pair<A,B>> zip(List<A> a, List<B> b)
      Zips two lists into a list of Pairs, truncated to the shorter length. 将两个列表合并为 Pair 列表,截断到较短长度。
      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,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. 使用组合函数合并两个列表,截断到较短长度。
      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

      public 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. 根据键提取器去重,保留首次出现的顺序。
      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

      public static <T> Map<T,Long> frequencies(Collection<T> collection)
      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

      public static <T> List<T> flatten(List<? extends List<T>> lists)
      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

      public static <T> Collector<T,?,List<T>> toUnmodifiableList()
      Returns a Collector that accumulates elements into an unmodifiable List. 返回将元素收集为不可修改 ListCollector

      Null elements are permitted.

      允许 null 元素。

      Type Parameters:
      T - the element type / 元素类型
      Returns:
      a collector producing an unmodifiable list / 产生不可修改列表的收集器
    • toUnmodifiableSet

      public static <T> Collector<T,?,Set<T>> toUnmodifiableSet()
      Returns a Collector that accumulates elements into an unmodifiable Set. 返回将元素收集为不可修改 SetCollector

      Null elements are permitted.

      允许 null 元素。

      Type Parameters:
      T - the element type / 元素类型
      Returns:
      a collector producing an unmodifiable set / 产生不可修改集合的收集器