Class HashMultiset<E>
java.lang.Object
java.util.AbstractCollection<E>
cloud.opencode.base.collections.HashMultiset<E>
- Type Parameters:
E- element type | 元素类型
- All Implemented Interfaces:
Multiset<E>, Serializable, Iterable<E>, Collection<E>
HashMultiset - Hash-based Multiset Implementation
HashMultiset - 基于哈希的多重集实现
A hash-based implementation of Multiset that maintains element counts using an internal HashMap.
基于哈希的 Multiset 实现,使用内部 HashMap 维护元素计数。
Features | 主要功能:
- O(1) count operations - O(1) 计数操作
- Null elements allowed - 允许空元素
- Serializable - 可序列化
- Live views - 实时视图
Usage Examples | 使用示例:
// Create empty Multiset - 创建空 Multiset
HashMultiset<String> multiset = HashMultiset.create();
// Create with initial elements - 创建带初始元素
HashMultiset<String> multiset = HashMultiset.create(Arrays.asList("a", "a", "b"));
// Create with initial capacity - 创建指定容量
HashMultiset<String> multiset = HashMultiset.create(16);
// Operations - 操作
multiset.add("apple", 3);
multiset.count("apple"); // 3
multiset.setCount("apple", 5);
Performance | 性能特性:
- add: O(1) average - add: O(1) 平均
- count: O(1) average - count: O(1) 平均
- remove: O(1) average - remove: O(1) 平均
- setCount: O(1) average - setCount: O(1) 平均
Security | 安全性:
- Thread-safe: No - 线程安全: 否
- Null-safe: Yes (allows null elements) - 空值安全: 是(允许空元素)
- 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> HashMultiset<E> create()Create an empty HashMultiset.static <E> HashMultiset<E> create(int initialCapacity) Create an empty HashMultiset with initial capacity.static <E> HashMultiset<E> create(E... elements) Create a HashMultiset from varargs.static <E> HashMultiset<E> Create a HashMultiset from an iterable.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 HashMultiset. 创建空 HashMultiset。- Type Parameters:
E- element type | 元素类型- Returns:
- new HashMultiset | 新的 HashMultiset
-
create
Create an empty HashMultiset with initial capacity. 创建指定容量的空 HashMultiset。- Type Parameters:
E- element type | 元素类型- Parameters:
initialCapacity- initial capacity | 初始容量- Returns:
- new HashMultiset | 新的 HashMultiset
-
create
Create a HashMultiset from an iterable. 从可迭代对象创建 HashMultiset。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- elements | 元素- Returns:
- new HashMultiset | 新的 HashMultiset
-
create
Create a HashMultiset from varargs. 从可变参数创建 HashMultiset。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- elements | 元素- Returns:
- new HashMultiset | 新的 HashMultiset
-
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:
element- 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>
-