Class OpenCollection

java.lang.Object
cloud.opencode.base.collections.OpenCollection

public final class OpenCollection extends Object
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 Type
    Method
    Description
    static <E> boolean
    addAll(Collection<E> collection, E... elements)
    Add all array elements to collection 批量添加数组元素
    static <E> boolean
    addAll(Collection<E> collection, Iterable<? extends E> iterable)
    Add all iterable elements to collection 批量添加 Iterable 元素
    static <E> boolean
    addAll(Collection<E> collection, Iterator<? extends E> iterator)
    Add all iterator elements to collection 批量添加 Iterator 元素
    static <E> boolean
    addIgnoreNull(Collection<E> collection, E object)
    Add element to collection if not null 如果元素不为 null 则添加
    static int
    cardinality(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(Iterable<I> input, Function<? super I, ? extends O> transformer)
    Collect transformed elements into a new collection 收集转换结果到新集合
    static boolean
    containsAll(Collection<?> coll, Collection<?> elements)
    Check if collection contains all the given elements 检查是否包含所有元素
    static boolean
    containsAny(Collection<?> coll, Object... elements)
    Check if collection contains any of the given elements 检查是否包含任一元素
    static <E> int
    countMatches(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> boolean
    exists(Iterable<E> input, Predicate<? super E> predicate)
    Check if any element matches the predicate 检查是否存在匹配元素
    static <E> E
    Extract the single element from a collection 从单元素集合提取元素
    static <E> boolean
    filter(Iterable<E> collection, Predicate<? super E> predicate)
    Filter collection in-place (retain matching elements) 原地过滤(保留匹配元素)
    static <E> boolean
    filterInverse(Iterable<E> collection, Predicate<? super E> predicate)
    Filter collection in-place (remove matching elements) 原地反向过滤(移除匹配元素)
    static <E> E
    find(Iterable<E> collection, Predicate<? super E> predicate)
    Find the first matching element 查找第一个匹配元素
    static <E> E
    get(Iterable<E> iterable, int index)
    Get the n-th element from an iterable 安全获取第 N 个元素
    static <E> Map<E,Integer>
    getCardinalityMap(Iterable<? extends E> coll)
    Get the cardinality map of an iterable 获取元素基数映射
    static <E> E
    getFirst(Iterable<E> iterable)
    Get the first element from an iterable 获取第一个元素
    static <E> E
    getLast(Iterable<E> iterable)
    Get 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 boolean
    isEmpty(Iterable<?> iterable)
    Check if iterable is empty 判断 Iterable 是否为空
    static boolean
    Check if collection is empty (null or empty) 判断集合是否为空(null 或 empty)
    static boolean
    isEmpty(Map<?,?> map)
    Check if map is empty (null or empty) 判断 Map 是否为空
    static boolean
    Compare two collections for equality (ignoring order) 比较两个集合是否相等(忽略顺序)
    static boolean
    Check if collection is not empty 判断集合是否非空
    static boolean
    isNotEmpty(Map<?,?> map)
    Check if map is not empty 判断 Map 是否非空
    static boolean
    Check if sub is a proper subset of sup 检查是否为真子集
    static boolean
    Check if sub is a subset of sup 检查是否为子集
    static <E> boolean
    matchesAll(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> boolean
    removeNulls(Collection<E> collection)
    Remove all null elements from collection 移除所有 null 元素
    static void
    Reverse an array in-place 反转数组
    static <E> Collection<E>
    select(Iterable<? extends E> input, Predicate<? super E> predicate)
    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 int
    size(Object object)
    Get the size of an object (collection, map, array, or iterable) 安全获取集合大小
    static <E> Collection<E>
    subtract(Iterable<? extends E> a, Iterable<? extends E> b)
    Returns the difference of two iterables (a - b) 返回两个可迭代对象的差集 (a - b)
    static <E> void
    transform(Collection<E> collection, UnaryOperator<E> transformer)
    Transform collection in-place 原地转换
    static <E> Collection<E>
    union(Iterable<? extends E> a, Iterable<? extends E> b)
    Returns the union of two iterables 返回两个可迭代对象的并集

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isEmpty

      public static boolean isEmpty(Collection<?> coll)
      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

      public static boolean isNotEmpty(Collection<?> coll)
      Check if collection is not empty 判断集合是否非空
      Parameters:
      coll - the collection to check | 要检查的集合
      Returns:
      true if not null and not empty | 如果非 null 且非空则返回 true
    • isEmpty

      public static boolean isEmpty(Map<?,?> map)
      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

      public static boolean isNotEmpty(Map<?,?> map)
      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

      public static boolean isEmpty(Iterable<?> iterable)
      Check if iterable is empty 判断 Iterable 是否为空
      Parameters:
      iterable - the iterable to check | 要检查的 Iterable
      Returns:
      true if null or empty | 如果为 null 或空则返回 true
    • union

      public static <E> Collection<E> union(Iterable<? extends E> a, Iterable<? extends E> b)
      Returns the union of two iterables 返回两个可迭代对象的并集
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      a - first iterable | 第一个可迭代对象
      b - second iterable | 第二个可迭代对象
      Returns:
      union collection | 并集
    • intersection

      public static <E> Collection<E> intersection(Iterable<? extends E> a, Iterable<? extends E> b)
      Returns the intersection of two iterables 返回两个可迭代对象的交集
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      a - first iterable | 第一个可迭代对象
      b - second iterable | 第二个可迭代对象
      Returns:
      intersection collection | 交集
    • subtract

      public static <E> Collection<E> subtract(Iterable<? extends E> a, Iterable<? extends E> b)
      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

      public 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)
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      a - first iterable | 第一个可迭代对象
      b - second iterable | 第二个可迭代对象
      Returns:
      symmetric difference collection | 对称差集
    • containsAny

      @SafeVarargs public static boolean containsAny(Collection<?> coll, Object... elements)
      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

      public static boolean containsAll(Collection<?> coll, Collection<?> elements)
      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

      public static boolean isEqualCollection(Collection<?> a, Collection<?> b)
      Compare two collections for equality (ignoring order) 比较两个集合是否相等(忽略顺序)
      Parameters:
      a - first collection | 第一个集合
      b - second collection | 第二个集合
      Returns:
      true if equal | 如果相等则返回 true
    • isSubCollection

      public static boolean isSubCollection(Collection<?> sub, Collection<?> sup)
      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

      public static boolean isProperSubCollection(Collection<?> sub, Collection<?> sup)
      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

      public static <E> Map<E,Integer> getCardinalityMap(Iterable<? extends E> coll)
      Get the cardinality map of an iterable 获取元素基数映射
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      coll - the iterable | 可迭代对象
      Returns:
      cardinality map | 基数映射
    • cardinality

      public static int cardinality(Object obj, Iterable<?> coll)
      Count the occurrences of an object in an iterable 统计元素出现次数
      Parameters:
      obj - the object to count | 要统计的对象
      coll - the iterable | 可迭代对象
      Returns:
      occurrence count | 出现次数
    • countMatches

      public static <E> int countMatches(Iterable<E> input, Predicate<? super E> predicate)
      Count elements matching the predicate 统计满足条件的元素数量
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      input - the iterable | 可迭代对象
      predicate - the predicate | 谓词
      Returns:
      match count | 匹配数量
    • filter

      public static <E> boolean filter(Iterable<E> collection, Predicate<? super E> predicate)
      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

      public static <E> boolean filterInverse(Iterable<E> collection, Predicate<? super E> predicate)
      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

      public static <E> Collection<E> select(Iterable<? extends E> input, Predicate<? super E> predicate)
      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

      public static <E> void transform(Collection<E> collection, UnaryOperator<E> transformer)
      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

      public static <E> E find(Iterable<E> collection, Predicate<? super E> predicate)
      Find the first matching element 查找第一个匹配元素
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      collection - the collection | 集合
      predicate - the predicate | 谓词
      Returns:
      first matching element or null | 第一个匹配元素或 null
    • exists

      public static <E> boolean exists(Iterable<E> input, Predicate<? super E> predicate)
      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

      public static <E> boolean matchesAll(Iterable<E> input, Predicate<? super E> predicate)
      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

      public static <E> E extractSingleton(Collection<E> collection)
      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

      @SafeVarargs public static <E> boolean addAll(Collection<E> collection, E... elements)
      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

      public static <E> boolean addAll(Collection<E> collection, Iterable<? extends E> iterable)
      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

      public static <E> boolean addAll(Collection<E> collection, Iterator<? extends E> iterator)
      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

      public static <E> boolean addIgnoreNull(Collection<E> collection, E object)
      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

      public static <E> boolean removeNulls(Collection<E> collection)
      Remove all null elements from collection 移除所有 null 元素
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      collection - the collection | 集合
      Returns:
      true if collection was modified | 如果集合被修改则返回 true
    • permutations

      public static <E> Collection<List<E>> permutations(Collection<E> collection)
      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

      public static void reverseArray(Object[] array)
      Reverse an array in-place 反转数组
      Parameters:
      array - the array to reverse | 要反转的数组
    • size

      public static int size(Object object)
      Get the size of an object (collection, map, array, or iterable) 安全获取集合大小
      Parameters:
      object - the object | 对象
      Returns:
      size or 0 if null | 大小或 0
    • get

      public static <E> E get(Iterable<E> iterable, int index)
      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

      public static <E> E getFirst(Iterable<E> iterable)
      Get the first element from an iterable 获取第一个元素
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      Returns:
      the first element or null | 第一个元素或 null
    • getLast

      public static <E> E getLast(Iterable<E> iterable)
      Get the last element from an iterable 获取最后一个元素
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      iterable - the iterable | 可迭代对象
      Returns:
      the last element or null | 最后一个元素或 null
    • emptyIfNull

      public static <E> Collection<E> emptyIfNull(Collection<E> collection)
      Return an empty collection if the input is null 空集合返回默认值
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      collection - the collection | 集合
      Returns:
      collection or empty list | 集合或空列表