Class ImmutableMultiset<E>
java.lang.Object
java.util.AbstractCollection<E>
cloud.opencode.base.collections.immutable.ImmutableMultiset<E>
- Type Parameters:
E- element type | 元素类型
- All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>
ImmutableMultiset - Immutable Multiset (Bag) Implementation
ImmutableMultiset - 不可变多重集(包)实现
A collection that supports order-independent equality and allows duplicate elements, tracking the count of each element. Cannot be modified after creation.
支持与顺序无关的相等性并允许重复元素的集合,跟踪每个元素的计数。创建后不能修改。
Features | 主要功能:
- Immutable - 不可变
- Thread-safe - 线程安全
- Null-safe (nulls not allowed) - 空值安全(不允许空值)
- Element counting - 元素计数
- O(1) count operations - O(1) 计数操作
Usage Examples | 使用示例:
// Create from elements - 从元素创建
ImmutableMultiset<String> multiset = ImmutableMultiset.of("a", "b", "a", "c", "b", "a");
int count = multiset.count("a"); // Returns 3 - 返回 3
// Create from collection - 从集合创建
ImmutableMultiset<String> multiset = ImmutableMultiset.copyOf(Arrays.asList("x", "x", "y"));
// Use builder - 使用构建器
ImmutableMultiset<String> multiset = ImmutableMultiset.<String>builder()
.add("a")
.addAll(Arrays.asList("b", "c"))
.setCount("a", 3)
.build();
// Query operations - 查询操作
Set<String> elementSet = multiset.elementSet(); // Unique elements - 唯一元素
int totalSize = multiset.size(); // Total count of all elements - 所有元素的总数
Performance | 性能特性:
- count: O(1) - count: O(1)
- contains: O(1) - contains: O(1)
- add/remove: Not supported (immutable) - add/remove: 不支持(不可变)
- iteration: O(n) where n is total size - iteration: O(n),其中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 ImmutableMultiset ImmutableMultiset 构建器static interfaceEntry interface for multiset entries 多重集条目的条目接口 -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) static <E> ImmutableMultiset.Builder<E> builder()Return a new builder.voidclear()booleanstatic <E> ImmutableMultiset<E> Return an immutable multiset containing the elements of the given iterable.static <E> ImmutableMultiset<E> copyOf(Collection<? extends E> elements) Return an immutable multiset containing the elements of the given collection.intReturn the count of the given element in this multiset.Return the set of distinct elements in this multiset.entrySet()Return the set of entries with elements and their counts.booleaninthashCode()booleanisEmpty()iterator()static <E> ImmutableMultiset<E> of()Return an empty immutable multiset.static <E> ImmutableMultiset<E> of(E e1) Return an immutable multiset containing the given element.static <E> ImmutableMultiset<E> of(E... elements) Return an immutable multiset containing the given elements.static <E> ImmutableMultiset<E> of(E e1, E e2) Return an immutable multiset containing the given elements.booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()Object[]toArray()<T> T[]toArray(T[] a) toString()Methods inherited from class AbstractCollection
containsAllMethods inherited from interface Collection
parallelStream, removeIf, spliterator, stream, toArray
-
Method Details
-
of
Return an empty immutable multiset. 返回空不可变多重集。- Type Parameters:
E- element type | 元素类型- Returns:
- empty immutable multiset | 空不可变多重集
-
of
Return an immutable multiset containing the given element. 返回包含给定元素的不可变多重集。- Type Parameters:
E- element type | 元素类型- Parameters:
e1- the element | 元素- Returns:
- immutable multiset | 不可变多重集
-
of
Return an immutable multiset containing the given elements. 返回包含给定元素的不可变多重集。- Type Parameters:
E- element type | 元素类型- Parameters:
e1- first element | 第一个元素e2- second element | 第二个元素- Returns:
- immutable multiset | 不可变多重集
-
of
Return an immutable multiset containing the given elements. 返回包含给定元素的不可变多重集。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- immutable multiset | 不可变多重集
-
copyOf
Return an immutable multiset containing the elements of the given collection. 返回包含给定集合元素的不可变多重集。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- immutable multiset | 不可变多重集
-
copyOf
Return an immutable multiset containing the elements of the given iterable. 返回包含给定可迭代对象元素的不可变多重集。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- immutable multiset | 不可变多重集
-
builder
Return a new builder. 返回新构建器。- Type Parameters:
E- element type | 元素类型- Returns:
- builder | 构建器
-
count
Return the count of the given element in this multiset. 返回此多重集中给定元素的计数。- Parameters:
element- the element to count | 要计数的元素- Returns:
- the count of the element, or 0 if not present | 元素的计数,如果不存在则为0
-
elementSet
-
entrySet
Return the set of entries with elements and their counts. 返回包含元素及其计数的条目集。- Returns:
- unmodifiable set of entries | 不可修改的条目集
-
iterator
-
size
public int size()- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
toArray
- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>
-
add
- Specified by:
addin interfaceCollection<E>- Overrides:
addin classAbstractCollection<E>
-
remove
- Specified by:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollection<E>
-
addAll
- Specified by:
addAllin interfaceCollection<E>- Overrides:
addAllin classAbstractCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>- Overrides:
removeAllin classAbstractCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>- Overrides:
retainAllin classAbstractCollection<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
-
equals
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<E>- Overrides:
hashCodein classObject
-
toString
- Overrides:
toStringin classAbstractCollection<E>
-