Class SetUtil

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

public final class SetUtil extends Object
SetUtil - Set Utility Class SetUtil - 集合工具类

Provides factory methods and set operations including union, intersection, difference, and symmetric difference.

提供集合工厂方法和集合运算,包括并集、交集、差集和对称差集。

Features | 主要功能:

  • Factory methods for set creation - 集合创建工厂方法
  • Set algebra operations (union, intersection, difference) - 集合代数运算
  • Power set and combinations - 幂集和组合
  • Cartesian product - 笛卡尔积

Usage Examples | 使用示例:

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

// Union view - 并集视图
SetView<String> union = SetUtil.union(set1, set2);

// Intersection view - 交集视图
SetView<String> intersection = SetUtil.intersection(set1, set2);

// Difference view - 差集视图
SetView<String> difference = SetUtil.difference(set1, set2);

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

Performance | 性能特性:

  • Factory methods: O(n) - 工厂方法: O(n)
  • Set operations: O(1) creation, O(n) iteration - 集合运算: O(1) 创建, O(n) 遍历
  • Power set: O(2^n) - 幂集: O(2^n)

Security | 安全性:

  • Thread-safe: No (except concurrent variants) - 线程安全: 否(并发变体除外)
  • 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 a new HashSet 创建 HashSet
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new HashSet | 新的 HashSet
    • newHashSet

      @SafeVarargs public static <E> HashSet<E> newHashSet(E... elements)
      Create a new HashSet with initial elements 创建带初始元素的 HashSet
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - initial elements | 初始元素
      Returns:
      new HashSet | 新的 HashSet
    • newHashSet

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

      public static <E> HashSet<E> newHashSetWithExpectedSize(int expectedSize)
      Create a new 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 a new LinkedHashSet 创建 LinkedHashSet
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new LinkedHashSet | 新的 LinkedHashSet
    • newLinkedHashSetWithExpectedSize

      public static <E> LinkedHashSet<E> newLinkedHashSetWithExpectedSize(int expectedSize)
      Create a new LinkedHashSet with expected size 创建指定容量的 LinkedHashSet
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      expectedSize - expected size | 预期大小
      Returns:
      new LinkedHashSet | 新的 LinkedHashSet
    • newTreeSet

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

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

      public static <E> CopyOnWriteArraySet<E> newCopyOnWriteArraySet()
      Create a new CopyOnWriteArraySet 创建 CopyOnWriteArraySet
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new CopyOnWriteArraySet | 新的 CopyOnWriteArraySet
    • newConcurrentHashSet

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

      public static <E> SetView<E> union(Set<? extends E> set1, Set<? extends E> set2)
      Return a union view of two sets 并集视图
      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)
      Return an intersection view of two sets 交集视图
      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)
      Return a difference view (set1 - set2) 差集视图
      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)
      Return a symmetric difference view 对称差集视图
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set1 - first set | 第一个集合
      set2 - second set | 第二个集合
      Returns:
      symmetric difference view | 对称差集视图
    • cartesianProduct

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

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

      public static <E> Set<Set<E>> powerSet(Set<E> set)
      Compute the power set (all subsets) 幂集(所有子集)
      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 specified size 指定大小的组合
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      size - combination size | 组合大小
      Returns:
      set of combinations | 组合集合
    • filter

      public static <E> Set<E> filter(Set<E> unfiltered, Predicate<? super E> predicate)
      Return a filtered view of a set 过滤视图
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      unfiltered - the unfiltered set | 未过滤的集合
      predicate - the predicate | 谓词
      Returns:
      filtered view | 过滤视图
    • filter

      public static <E> SortedSet<E> filter(SortedSet<E> unfiltered, Predicate<? super E> predicate)
      Return a filtered view of a SortedSet 过滤 SortedSet 视图
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      unfiltered - the unfiltered set | 未过滤的集合
      predicate - the predicate | 谓词
      Returns:
      filtered view | 过滤视图
    • filter

      public static <E> NavigableSet<E> filter(NavigableSet<E> unfiltered, Predicate<? super E> predicate)
      Return a filtered view of a NavigableSet 过滤 NavigableSet 视图
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      unfiltered - the unfiltered set | 未过滤的集合
      predicate - the predicate | 谓词
      Returns:
      filtered view | 过滤视图
    • synchronizedSet

      public static <E> Set<E> synchronizedSet(Set<E> set)
      Return a synchronized set 同步包装
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      Returns:
      synchronized set | 同步集合
    • unmodifiableSet

      public static <E> Set<E> unmodifiableSet(Set<? extends E> set)
      Return an unmodifiable set 不可修改包装
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      Returns:
      unmodifiable set | 不可修改集合
    • unmodifiableNavigableSet

      public static <E> NavigableSet<E> unmodifiableNavigableSet(NavigableSet<E> set)
      Return an unmodifiable NavigableSet NavigableSet 视图
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      Returns:
      unmodifiable NavigableSet | 不可修改的 NavigableSet
    • synchronizedNavigableSet

      public static <E> NavigableSet<E> synchronizedNavigableSet(NavigableSet<E> set)
      Return a synchronized NavigableSet 同步 NavigableSet
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      set - the set | 集合
      Returns:
      synchronized NavigableSet | 同步的 NavigableSet