Class OpenSet

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

public final class OpenSet extends Object
OpenSet - Set Facade Utility Class OpenSet - Set 门面工具类

Provides comprehensive set operations including creation, set algebra, filtering, and querying.

提供全面的 Set 操作,包括创建、集合代数、过滤和查询。

Features | 主要功能:

  • Factory methods for set creation - Set 创建工厂方法
  • Set algebra operations - 集合代数运算
  • Set filtering - Set 过滤
  • Power set and combinations - 幂集和组合

Usage Examples | 使用示例:

// Create set - 创建 Set
Set<String> set = OpenSet.of("a", "b", "c");

// Union - 并集
SetView<String> union = OpenSet.union(set1, set2);

// Intersection - 交集
SetView<String> intersection = OpenSet.intersection(set1, set2);

// Power set - 幂集
Set<Set<String>> powerSet = OpenSet.powerSet(set);

Performance | 性能特性:

  • Most operations: O(n) - 大多数操作: O(n)
  • Contains: O(1) for HashSet - 包含: HashSet 为 O(1)
  • Power set: O(2^n) - 幂集: O(2^n)

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • newHashSet

      public static <E> HashSet<E> newHashSet()
      Create an empty HashSet. 创建空的 HashSet。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new HashSet | 新的 HashSet
    • of

      @SafeVarargs public static <E> HashSet<E> of(E... elements)
      Create a HashSet with elements. 创建包含元素的 HashSet。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      new HashSet | 新的 HashSet
    • from

      public static <E> HashSet<E> from(Iterable<? extends E> elements)
      Create a HashSet from an Iterable. 从 Iterable 创建 HashSet。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      new HashSet | 新的 HashSet
    • withExpectedSize

      public static <E> HashSet<E> withExpectedSize(int expectedSize)
      Create a HashSet with expected size. 创建具有预期大小的 HashSet。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      expectedSize - expected size | 预期大小
      Returns:
      new HashSet | 新的 HashSet
    • newLinkedHashSet

      public static <E> LinkedHashSet<E> newLinkedHashSet()
      Create an empty LinkedHashSet. 创建空的 LinkedHashSet。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new LinkedHashSet | 新的 LinkedHashSet
    • newTreeSet

      public static <E extends Comparable<E>> TreeSet<E> newTreeSet()
      Create an empty TreeSet. 创建空的 TreeSet。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new TreeSet | 新的 TreeSet
    • newTreeSet

      public static <E> TreeSet<E> newTreeSet(Comparator<? super E> comparator)
      Create a TreeSet with comparator. 创建具有比较器的 TreeSet。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      comparator - the comparator | 比较器
      Returns:
      new TreeSet | 新的 TreeSet
    • newConcurrentHashSet

      public static <E> Set<E> newConcurrentHashSet()
      Create a concurrent hash set. 创建并发哈希集合。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new concurrent set | 新的并发 Set
    • union

      public static <E> SetView<E> union(Set<? extends E> set1, Set<? extends E> set2)
      Compute union of two sets as a SetView. 计算两个集合的并集视图。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set1 - first set | 第一个集合
      set2 - second set | 第二个集合
      Returns:
      union view | 并集视图
    • intersection

      public static <E> SetView<E> intersection(Set<E> set1, Set<?> set2)
      Compute intersection of two sets as a SetView. 计算两个集合的交集视图。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set1 - first set | 第一个集合
      set2 - second set | 第二个集合
      Returns:
      intersection view | 交集视图
    • difference

      public static <E> SetView<E> difference(Set<E> set1, Set<?> set2)
      Compute difference of two sets as a SetView. 计算两个集合的差集视图。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set1 - first set | 第一个集合
      set2 - second set | 第二个集合
      Returns:
      difference view | 差集视图
    • symmetricDifference

      public static <E> SetView<E> symmetricDifference(Set<? extends E> set1, Set<? extends E> set2)
      Compute symmetric difference of two sets as a SetView. 计算两个集合的对称差集视图。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set1 - first set | 第一个集合
      set2 - second set | 第二个集合
      Returns:
      symmetric difference view | 对称差集视图
    • powerSet

      public static <E> Set<Set<E>> powerSet(Set<E> set)
      Compute power set (all subsets) of a set. 计算集合的幂集(所有子集)。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      Returns:
      power set | 幂集
    • combinations

      public static <E> Set<Set<E>> combinations(Set<E> set, int size)
      Compute combinations of given size. 计算给定大小的组合。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      size - combination size | 组合大小
      Returns:
      set of combinations | 组合集合
    • cartesianProduct

      @SafeVarargs public static <E> Set<List<E>> cartesianProduct(Set<? extends E>... sets)
      Compute Cartesian product of sets. 计算集合的笛卡尔积。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      sets - the sets | 集合
      Returns:
      Cartesian product | 笛卡尔积
    • filter

      public static <E> Set<E> filter(Set<E> set, Predicate<? super E> predicate)
      Filter set by predicate. 按谓词过滤集合。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      predicate - the predicate | 谓词
      Returns:
      filtered set | 过滤后的集合
    • algebra

      public static <E> SetAlgebra<E> algebra(Set<E> set)
      Get the SetAlgebra interface for the set. 获取集合的 SetAlgebra 接口。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      Returns:
      SetAlgebra instance | SetAlgebra 实例
    • disjoint

      public static boolean disjoint(Set<?> set1, Set<?> set2)
      Check if sets are disjoint. 检查集合是否不相交。
      Parameters:
      set1 - first set | 第一个集合
      set2 - second set | 第二个集合
      Returns:
      true if disjoint | 如果不相交则返回 true
    • isSubset

      public static boolean isSubset(Set<?> subset, Set<?> superset)
      Check if one set is a subset of another. 检查一个集合是否为另一个的子集。
      Parameters:
      subset - potential subset | 潜在子集
      superset - potential superset | 潜在超集
      Returns:
      true if subset | 如果是子集则返回 true
    • equals

      public static boolean equals(Set<?> set1, Set<?> set2)
      Check if sets are equal (same elements). 检查集合是否相等(相同元素)。
      Parameters:
      set1 - first set | 第一个集合
      set2 - second set | 第二个集合
      Returns:
      true if equal | 如果相等则返回 true