Class PersistentSet<E>
java.lang.Object
cloud.opencode.base.collections.immutable.PersistentSet<E>
- Type Parameters:
E- element type | 元素类型
- All Implemented Interfaces:
Serializable, Iterable<E>
PersistentSet - Persistent immutable set based on
PersistentMap
PersistentSet - 基于 PersistentMap 的持久化不可变集合
All mutation operations (add, remove, set algebra) return
a new PersistentSet while sharing structure with the original via
the underlying HAMT-based PersistentMap.
所有变更操作(add、remove、集合代数)均返回新的
PersistentSet,同时通过底层基于 HAMT 的 PersistentMap
与原集合共享结构。
Features | 主要功能:
- Structural sharing via HAMT - 基于 HAMT 的结构共享
- Immutable - 不可变
- O(log32 n) add/remove/contains - O(log32 n) 的添加/删除/查询
- Set algebra: union, intersection, difference - 集合代数:并集、交集、差集
Usage Examples | 使用示例:
// Create set - 创建集合
PersistentSet<String> set = PersistentSet.of("a", "b", "c");
// Add element (returns new set) - 添加元素(返回新集合)
PersistentSet<String> set2 = set.add("d");
// set still has size 3, set2 has size 4
// Set algebra - 集合代数
PersistentSet<String> other = PersistentSet.of("b", "c", "d");
PersistentSet<String> union = set.union(other); // {a, b, c, d}
PersistentSet<String> inter = set.intersection(other); // {b, c}
PersistentSet<String> diff = set.difference(other); // {a}
Performance | 性能特性:
- add: O(log32 n) - add: O(log32 n)
- remove: O(log32 n) - remove: O(log32 n)
- contains: O(log32 n) - contains: O(log32 n)
- union/intersection/difference: O(n + m) - union/intersection/difference: O(n + m)
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Since:
- JDK 25, opencode-base-collections V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturn a new set with the given element added.booleanCheck if this set contains the given element.difference(PersistentSet<E> other) Return the difference of this set minus another set.static <E> PersistentSet<E> empty()Return an empty persistent set.booleanstatic <E> PersistentSet<E> Create a persistent set from an iterable.inthashCode()intersection(PersistentSet<E> other) Return the intersection of this set and another set.booleanisEmpty()Check if this set is empty.iterator()Return an iterator over the elements of this set.static <E> PersistentSet<E> of(E... elements) Create a persistent set from the given elements.Return a new set with the given element removed.intsize()Return the number of elements in this set.stream()Return a sequential stream over the elements.toSet()Convert this persistent set to a JDKSet.toString()union(PersistentSet<E> other) Return the union of this set and another set.Methods inherited from interface Iterable
forEach, spliterator
-
Method Details
-
empty
Return an empty persistent set. 返回一个空的持久化集合。- Type Parameters:
E- element type | 元素类型- Returns:
- empty persistent set | 空的持久化集合
-
of
Create a persistent set from the given elements. 从给定的元素创建持久化集合。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- persistent set containing the elements | 包含元素的持久化集合
-
from
Create a persistent set from an iterable. 从可迭代对象创建持久化集合。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the iterable | 可迭代对象- Returns:
- persistent set containing the elements | 包含元素的持久化集合
-
add
Return a new set with the given element added. 返回添加给定元素后的新集合。If the element is already present, returns
this.如果元素已存在,返回
this。- Parameters:
element- the element to add | 要添加的元素- Returns:
- a new set containing the element | 包含该元素的新集合
-
remove
Return a new set with the given element removed. 返回删除给定元素后的新集合。If the element is not present, returns
this.如果元素不存在,返回
this。- Parameters:
element- the element to remove | 要删除的元素- Returns:
- a new set without the element | 不包含该元素的新集合
-
contains
Check if this set contains the given element. 检查此集合是否包含给定元素。- Parameters:
element- the element to search for | 要搜索的元素- Returns:
- true if the element is present | 如果元素存在则返回 true
-
size
public int size()Return the number of elements in this set. 返回此集合中的元素数量。- Returns:
- the size | 大小
-
isEmpty
public boolean isEmpty()Check if this set is empty. 检查此集合是否为空。- Returns:
- true if the set is empty | 如果集合为空则返回 true
-
union
Return the union of this set and another set. 返回此集合与另一个集合的并集。- Parameters:
other- the other set | 另一个集合- Returns:
- a new set containing all elements from both sets | 包含两个集合所有元素的新集合
-
intersection
Return the intersection of this set and another set. 返回此集合与另一个集合的交集。- Parameters:
other- the other set | 另一个集合- Returns:
- a new set containing only elements present in both sets | 仅包含两个集合共有元素的新集合
-
difference
Return the difference of this set minus another set. 返回此集合减去另一个集合的差集。- Parameters:
other- the other set | 另一个集合- Returns:
- a new set containing elements in this set but not in the other | 包含在此集合中但不在另一个集合中的元素的新集合
-
iterator
-
stream
-
toSet
-
equals
-
hashCode
-
toString
-