Class PersistentSet<E>

java.lang.Object
cloud.opencode.base.collections.immutable.PersistentSet<E>
Type Parameters:
E - element type | 元素类型
All Implemented Interfaces:
Serializable, Iterable<E>

public final class PersistentSet<E> extends Object implements Iterable<E>, Serializable
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.

所有变更操作(addremove、集合代数)均返回新的 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 Details

    • empty

      public static <E> PersistentSet<E> empty()
      Return an empty persistent set. 返回一个空的持久化集合。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      empty persistent set | 空的持久化集合
    • of

      @SafeVarargs public static <E> PersistentSet<E> of(E... elements)
      Create a persistent set from the given elements. 从给定的元素创建持久化集合。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      persistent set containing the elements | 包含元素的持久化集合
    • from

      public static <E> PersistentSet<E> from(Iterable<? extends E> elements)
      Create a persistent set from an iterable. 从可迭代对象创建持久化集合。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the iterable | 可迭代对象
      Returns:
      persistent set containing the elements | 包含元素的持久化集合
    • add

      public PersistentSet<E> add(E element)
      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

      public PersistentSet<E> remove(E element)
      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

      public boolean contains(E element)
      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

      public PersistentSet<E> union(PersistentSet<E> other)
      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

      public PersistentSet<E> intersection(PersistentSet<E> other)
      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

      public PersistentSet<E> difference(PersistentSet<E> other)
      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

      public Iterator<E> iterator()
      Return an iterator over the elements of this set. 返回此集合元素上的迭代器。
      Specified by:
      iterator in interface Iterable<E>
      Returns:
      an iterator | 迭代器
    • stream

      public Stream<E> stream()
      Return a sequential stream over the elements. 返回元素上的顺序流。
      Returns:
      a stream | 流
    • toSet

      public Set<E> toSet()
      Convert this persistent set to a JDK Set. 将此持久化集合转换为 JDK Set
      Returns:
      an unmodifiable set containing all elements | 包含所有元素的不可修改集合
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object