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>
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for ImmutableSet ImmutableSet 构建器 -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) static <E> ImmutableSet.Builder<E> builder()Return a new builder.voidclear()booleanstatic <E> ImmutableSet<E> Return an immutable set containing the elements of the given iterable.static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements) Return an immutable set containing the elements of the given collection.iterator()static <E> ImmutableSet<E> of()Return an empty immutable set.static <E> ImmutableSet<E> of(E e1) Return an immutable set containing the given element.static <E> ImmutableSet<E> of(E... elements) Return an immutable set containing the given elements.static <E> ImmutableSet<E> of(E e1, E e2) Return an immutable set containing the given elements.booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()Object[]toArray()<T> T[]toArray(T[] a) Methods inherited from class AbstractSet
equals, hashCodeMethods inherited from class AbstractCollection
containsAll, isEmpty, toStringMethods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface Set
containsAll, isEmpty, spliterator
-
Method Details
-
of
Return an empty immutable set. 返回空不可变集合。- Type Parameters:
E- element type | 元素类型- Returns:
- empty immutable set | 空不可变集合
-
of
Return an immutable set containing the given element. 返回包含给定元素的不可变集合。- Type Parameters:
E- element type | 元素类型- Parameters:
e1- the element | 元素- Returns:
- immutable set | 不可变集合
-
of
Return an immutable set containing the given elements. 返回包含给定元素的不可变集合。- Type Parameters:
E- element type | 元素类型- Parameters:
e1- first element | 第一个元素e2- second element | 第二个元素- Returns:
- immutable set | 不可变集合
-
of
Return an immutable set containing the given elements. 返回包含给定元素的不可变集合。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- immutable set | 不可变集合
-
copyOf
Return an immutable set containing the elements of the given collection. 返回包含给定集合元素的不可变集合。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- immutable set | 不可变集合
-
copyOf
Return an immutable set containing the elements of the given iterable. 返回包含给定可迭代对象元素的不可变集合。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- immutable set | 不可变集合
-
builder
Return a new builder. 返回新构建器。- Type Parameters:
E- element type | 元素类型- Returns:
- builder | 构建器
-
iterator
-
size
public int size()- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>
-
toArray
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
add
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>
-
remove
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>
-
addAll
- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceSet<E>- Overrides:
addAllin classAbstractCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceSet<E>- Overrides:
removeAllin classAbstractSet<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceSet<E>- Overrides:
retainAllin classAbstractCollection<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-