Class SetUtil
java.lang.Object
cloud.opencode.base.collections.SetUtil
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 Summary
Modifier and TypeMethodDescriptioncartesianProduct(List<? extends Set<? extends E>> sets) Compute the Cartesian product of sets 笛卡尔积cartesianProduct(Set<? extends E>... sets) Compute the Cartesian product of sets 笛卡尔积combinations(Set<E> set, int size) Compute combinations of specified size 指定大小的组合static <E> SetView<E> difference(Set<E> set1, Set<?> set2) Return a difference view (set1 - set2) 差集视图static <E> NavigableSet<E> filter(NavigableSet<E> unfiltered, Predicate<? super E> predicate) Return a filtered view of a NavigableSet 过滤 NavigableSet 视图static <E> Set<E> Return a filtered view of a set 过滤视图static <E> SortedSet<E> Return a filtered view of a SortedSet 过滤 SortedSet 视图static <E> SetView<E> intersection(Set<E> set1, Set<?> set2) Return an intersection view of two sets 交集视图static <E> Set<E> Create a new concurrent hash set 创建 ConcurrentHashSetstatic <E> CopyOnWriteArraySet<E> Create a new CopyOnWriteArraySet 创建 CopyOnWriteArraySetstatic <E> HashSet<E> Create a new HashSet 创建 HashSetstatic <E> HashSet<E> newHashSet(E... elements) Create a new HashSet with initial elements 创建带初始元素的 HashSetstatic <E> HashSet<E> newHashSet(Iterable<? extends E> elements) Create a new HashSet from an Iterable 从 Iterable 创建 HashSetstatic <E> HashSet<E> newHashSetWithExpectedSize(int expectedSize) Create a new HashSet with expected size 创建指定容量的 HashSetstatic <E> LinkedHashSet<E> Create a new LinkedHashSet 创建 LinkedHashSetstatic <E> LinkedHashSet<E> newLinkedHashSetWithExpectedSize(int expectedSize) Create a new LinkedHashSet with expected size 创建指定容量的 LinkedHashSetstatic <E extends Comparable<E>>
TreeSet<E> Create a new TreeSet 创建 TreeSetstatic <E> TreeSet<E> newTreeSet(Comparator<? super E> comparator) Create a new TreeSet with comparator 带比较器的 TreeSetCompute the power set (all subsets) 幂集(所有子集)static <E> SetView<E> symmetricDifference(Set<? extends E> set1, Set<? extends E> set2) Return a symmetric difference view 对称差集视图static <E> NavigableSet<E> synchronizedNavigableSet(NavigableSet<E> set) Return a synchronized NavigableSet 同步 NavigableSetstatic <E> Set<E> synchronizedSet(Set<E> set) Return a synchronized set 同步包装static <E> SetView<E> Return a union view of two sets 并集视图static <E> NavigableSet<E> unmodifiableNavigableSet(NavigableSet<E> set) Return an unmodifiable NavigableSet NavigableSet 视图static <E> Set<E> unmodifiableSet(Set<? extends E> set) Return an unmodifiable set 不可修改包装
-
Method Details
-
newHashSet
Create a new HashSet 创建 HashSet- Type Parameters:
E- element type | 元素类型- Returns:
- new HashSet | 新的 HashSet
-
newHashSet
Create a new HashSet with initial elements 创建带初始元素的 HashSet- Type Parameters:
E- element type | 元素类型- Parameters:
elements- initial elements | 初始元素- Returns:
- new HashSet | 新的 HashSet
-
newHashSet
-
newHashSetWithExpectedSize
Create a new HashSet with expected size 创建指定容量的 HashSet- Type Parameters:
E- element type | 元素类型- Parameters:
expectedSize- expected size | 预期大小- Returns:
- new HashSet | 新的 HashSet
-
newLinkedHashSet
Create a new LinkedHashSet 创建 LinkedHashSet- Type Parameters:
E- element type | 元素类型- Returns:
- new LinkedHashSet | 新的 LinkedHashSet
-
newLinkedHashSetWithExpectedSize
Create a new LinkedHashSet with expected size 创建指定容量的 LinkedHashSet- Type Parameters:
E- element type | 元素类型- Parameters:
expectedSize- expected size | 预期大小- Returns:
- new LinkedHashSet | 新的 LinkedHashSet
-
newTreeSet
Create a new TreeSet 创建 TreeSet- Type Parameters:
E- element type | 元素类型- Returns:
- new TreeSet | 新的 TreeSet
-
newTreeSet
Create a new TreeSet with comparator 带比较器的 TreeSet- Type Parameters:
E- element type | 元素类型- Parameters:
comparator- the comparator | 比较器- Returns:
- new TreeSet | 新的 TreeSet
-
newCopyOnWriteArraySet
Create a new CopyOnWriteArraySet 创建 CopyOnWriteArraySet- Type Parameters:
E- element type | 元素类型- Returns:
- new CopyOnWriteArraySet | 新的 CopyOnWriteArraySet
-
newConcurrentHashSet
Create a new concurrent hash set 创建 ConcurrentHashSet- Type Parameters:
E- element type | 元素类型- Returns:
- new concurrent set | 新的并发集合
-
union
-
intersection
-
difference
-
symmetricDifference
-
cartesianProduct
-
cartesianProduct
Compute the Cartesian product of sets 笛卡尔积- Type Parameters:
E- element type | 元素类型- Parameters:
sets- sets to compute product | 要计算积的集合- Returns:
- Cartesian product | 笛卡尔积
-
powerSet
-
combinations
-
filter
-
filter
Return a filtered view of a SortedSet 过滤 SortedSet 视图- Type Parameters:
E- element type | 元素类型- Parameters:
unfiltered- the unfiltered set | 未过滤的集合predicate- the predicate | 谓词- Returns:
- filtered view | 过滤视图
-
synchronizedSet
-
unmodifiableSet
-