Class ImmutableSet<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
cloud.opencode.base.collections.ImmutableSet<E>
Type Parameters:
E - element type | 元素类型
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>

public final class ImmutableSet<E> extends AbstractSet<E> implements Serializable
ImmutableSet - Immutable Set Implementation ImmutableSet - 不可变集合实现

A set that cannot be modified after creation. Any attempt to modify the set will throw an exception.

创建后不能修改的集合。任何修改集合的尝试都会抛出异常。

Features | 主要功能:

  • Immutable - 不可变
  • Thread-safe - 线程安全
  • Null-safe (nulls not allowed) - 空值安全(不允许空值)
  • O(1) contains check - O(1) 包含检查

Usage Examples | 使用示例:

// Create from elements - 从元素创建
ImmutableSet<String> set = ImmutableSet.of("a", "b", "c");

// Create from collection - 从集合创建
ImmutableSet<String> set = ImmutableSet.copyOf(existingSet);

// Use builder - 使用构建器
ImmutableSet<String> set = ImmutableSet.<String>builder()
    .add("a")
    .addAll(Arrays.asList("b", "c"))
    .build();

Performance | 性能特性:

  • contains: O(1) average - contains: O(1) 平均
  • iteration: O(n) - iteration: O(n)

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Yes (nulls not allowed) - 空值安全: 是(不允许空值)
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static <E> ImmutableSet<E> of()
      Return an empty immutable set. 返回空不可变集合。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      empty immutable set | 空不可变集合
    • of

      public static <E> ImmutableSet<E> of(E e1)
      Return an immutable set containing the given element. 返回包含给定元素的不可变集合。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      e1 - the element | 元素
      Returns:
      immutable set | 不可变集合
    • of

      public static <E> ImmutableSet<E> of(E e1, E e2)
      Return an immutable set containing the given elements. 返回包含给定元素的不可变集合。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      e1 - first element | 第一个元素
      e2 - second element | 第二个元素
      Returns:
      immutable set | 不可变集合
    • of

      @SafeVarargs public static <E> ImmutableSet<E> of(E... elements)
      Return an immutable set containing the given elements. 返回包含给定元素的不可变集合。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      immutable set | 不可变集合
    • copyOf

      public static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements)
      Return an immutable set containing the elements of the given collection. 返回包含给定集合元素的不可变集合。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      immutable set | 不可变集合
    • copyOf

      public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements)
      Return an immutable set containing the elements of the given iterable. 返回包含给定可迭代对象元素的不可变集合。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      immutable set | 不可变集合
    • builder

      public static <E> ImmutableSet.Builder<E> builder()
      Return a new builder. 返回新构建器。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      builder | 构建器
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
      Specified by:
      iterator in class AbstractCollection<E>
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
      Specified by:
      size in class AbstractCollection<E>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
      Overrides:
      contains in class AbstractCollection<E>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class AbstractCollection<E>
    • toArray

      public <T> T[] toArray(T[] a)
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class AbstractCollection<E>
    • add

      public boolean add(E e)
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class AbstractCollection<E>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
      Overrides:
      remove in class AbstractCollection<E>
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface Set<E>
      Overrides:
      addAll in class AbstractCollection<E>
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface Set<E>
      Overrides:
      removeAll in class AbstractSet<E>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<E>
      Specified by:
      retainAll in interface Set<E>
      Overrides:
      retainAll in class AbstractCollection<E>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
      Overrides:
      clear in class AbstractCollection<E>