Class ConcurrentHashMultiset<E>
java.lang.Object
java.util.AbstractCollection<E>
cloud.opencode.base.collections.specialized.ConcurrentHashMultiset<E>
- Type Parameters:
E- element type | 元素类型
- All Implemented Interfaces:
Multiset<E>, Serializable, Iterable<E>, Collection<E>
public final class ConcurrentHashMultiset<E>
extends AbstractCollection<E>
implements Multiset<E>, Serializable
ConcurrentHashMultiset - Thread-safe Multiset Implementation
ConcurrentHashMultiset - 线程安全的多重集合实现
A thread-safe multiset implementation using ConcurrentHashMap.
使用 ConcurrentHashMap 的线程安全多重集合实现。
Features | 主要功能:
- Thread-safe - 线程安全
- High concurrency - 高并发
- Lock-free reads - 无锁读取
- Atomic updates - 原子更新
Usage Examples | 使用示例:
ConcurrentHashMultiset<String> multiset = ConcurrentHashMultiset.create();
// Thread-safe operations - 线程安全操作
multiset.add("apple", 3);
multiset.add("banana");
// Atomic count operations - 原子计数操作
int count = multiset.count("apple"); // 3
// Can be safely used from multiple threads - 可安全地从多个线程使用
ExecutorService executor = Executors.newFixedThreadPool(4);
for (int i = 0; i < 100; i++) {
executor.submit(() -> multiset.add("concurrent"));
}
Performance | 性能特性:
- add: O(1) average - add: O(1) 平均
- remove: O(1) average - remove: O(1) 平均
- count: O(1) - count: O(1)
- contains: O(1) - contains: O(1)
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: No (nulls not allowed) - 空值安全: 否(不允许空值)
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface Multiset
Multiset.Entry<E> -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd one occurrence of the element.intAdd a number of occurrences of an element.voidclear()booleanCheck if multiset contains the element.intReturn the count of the specified element.static <E> ConcurrentHashMultiset<E> create()Create an empty ConcurrentHashMultiset.static <E> ConcurrentHashMultiset<E> create(int initialCapacity) Create a ConcurrentHashMultiset with initial capacity.static <E> ConcurrentHashMultiset<E> Create a ConcurrentHashMultiset from elements.Return the set of distinct elements.entrySet()Return the set of entries (element with count).booleaninthashCode()booleanisEmpty()iterator()booleanRemove one occurrence of the element.intRemove a number of occurrences of an element.intSet the count of an element.booleanConditionally set the count of an element.intsize()Return the total count of all elements.toString()Methods inherited from class AbstractCollection
addAll, containsAll, removeAll, retainAll, toArray, toArrayMethods inherited from interface Collection
addAll, containsAll, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Method Details
-
create
Create an empty ConcurrentHashMultiset. 创建空 ConcurrentHashMultiset。- Type Parameters:
E- element type | 元素类型- Returns:
- new empty ConcurrentHashMultiset | 新空 ConcurrentHashMultiset
-
create
Create a ConcurrentHashMultiset with initial capacity. 创建指定初始容量的 ConcurrentHashMultiset。- Type Parameters:
E- element type | 元素类型- Parameters:
initialCapacity- initial capacity | 初始容量- Returns:
- new empty ConcurrentHashMultiset | 新空 ConcurrentHashMultiset
-
create
Create a ConcurrentHashMultiset from elements. 从元素创建 ConcurrentHashMultiset。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- new ConcurrentHashMultiset | 新 ConcurrentHashMultiset
-
count
-
add
Description copied from interface:MultisetAdd a number of occurrences of an element. 添加元素的多个出现。 -
remove
Description copied from interface:MultisetRemove a number of occurrences of an element. 移除元素的多个出现。 -
setCount
-
setCount
Description copied from interface:MultisetConditionally set the count of an element. 条件性地设置元素的计数。 -
elementSet
-
entrySet
-
size
public int size()Description copied from interface:MultisetReturn the total count of all elements. 返回所有元素的总计数。- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceMultiset<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- total size | 总大小
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
contains
Description copied from interface:MultisetCheck if multiset contains the element. 检查多重集是否包含元素。- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceMultiset<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
o- the element | 元素- Returns:
- true if contains | 如果包含则返回 true
-
add
Description copied from interface:MultisetAdd one occurrence of the element. 添加元素的一个出现。- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceMultiset<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
element- the element | 元素- Returns:
- always true | 始终为 true
-
remove
Description copied from interface:MultisetRemove one occurrence of the element. 移除元素的一个出现。- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceMultiset<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
element- the element | 元素- Returns:
- true if element was present | 如果元素存在则返回 true
-
iterator
-
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>
-