Interface Multiset<E>
- Type Parameters:
E- element type | 元素类型
- All Superinterfaces:
Collection<E>, Iterable<E>
- All Known Subinterfaces:
NavigableMultiset<E>
- All Known Implementing Classes:
AbstractMultiset, ConcurrentHashMultiset, HashMultiset, LinkedHashMultiset, TreeMultiset
Multiset - Collection that counts element occurrences
Multiset - 计数元素出现次数的集合
A collection that supports order-independent equality, like Set, but may have duplicate elements. Also known as a bag or multiset.
支持与顺序无关的相等性的集合,类似于 Set,但可以有重复元素。也称为袋或多重集。
Features | 主要功能:
- Count element occurrences - 计算元素出现次数
- Add/remove multiple occurrences - 添加/移除多个出现
- Set element count directly - 直接设置元素计数
- Element set view (distinct elements) - 元素集合视图(去重元素)
- Entry set view (element with count) - 条目集合视图(带计数的元素)
Usage Examples | 使用示例:
// Create Multiset - 创建 Multiset
Multiset<String> multiset = HashMultiset.create();
// Add elements - 添加元素
multiset.add("apple");
multiset.add("apple", 3); // add 3 more apples
// Count elements - 计算元素
int count = multiset.count("apple"); // 4
// Set count directly - 直接设置计数
multiset.setCount("banana", 5);
// Get distinct elements - 获取去重元素
Set<String> elements = multiset.elementSet();
// Iterate with counts - 带计数迭代
for (Multiset.Entry<String> entry : multiset.entrySet()) {
System.out.println(entry.getElement() + ": " + entry.getCount());
}
Performance | 性能特性:
- add: O(1) for hash-based - add: O(1) 基于哈希
- count: O(1) for hash-based - count: O(1) 基于哈希
- remove: O(1) for hash-based - remove: O(1) 基于哈希
Security | 安全性:
- Thread-safe: Implementation dependent - 线程安全: 取决于实现
- Null-safe: Implementation dependent - 空值安全: 取决于实现
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceEntry - Element with occurrence count Entry - 带出现次数的元素 -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd one occurrence of the element.intAdd a number of occurrences of an element.booleanCheck if multiset contains the element.intReturn the count of the specified element.Return the set of distinct elements.entrySet()Return the set of entries (element with count).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.Methods inherited from interface Collection
addAll, clear, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Method Details
-
count
Return the count of the specified element. 返回指定元素的计数。- Parameters:
element- the element to count | 要计数的元素- Returns:
- the count (0 if not present) | 计数(不存在则为 0)
-
add
Add a number of occurrences of an element. 添加元素的多个出现。- Parameters:
element- the element to add | 要添加的元素occurrences- number of occurrences to add | 要添加的出现次数- Returns:
- previous count | 之前的计数
- Throws:
IllegalArgumentException- if occurrences is negative | 如果出现次数为负
-
remove
Remove a number of occurrences of an element. 移除元素的多个出现。- Parameters:
element- the element to remove | 要移除的元素occurrences- number of occurrences to remove | 要移除的出现次数- Returns:
- previous count | 之前的计数
- Throws:
IllegalArgumentException- if occurrences is negative | 如果出现次数为负
-
setCount
Set the count of an element. 设置元素的计数。- Parameters:
element- the element | 元素count- the new count | 新计数- Returns:
- previous count | 之前的计数
- Throws:
IllegalArgumentException- if count is negative | 如果计数为负
-
setCount
Conditionally set the count of an element. 条件性地设置元素的计数。- Parameters:
element- the element | 元素oldCount- expected current count | 期望的当前计数newCount- the new count | 新计数- Returns:
- true if count was changed | 如果计数被更改则返回 true
- Throws:
IllegalArgumentException- if counts are negative | 如果计数为负
-
elementSet
-
entrySet
Set<Multiset.Entry<E>> entrySet()Return the set of entries (element with count). 返回条目集合(带计数的元素)。- Returns:
- entry set | 条目集合
-
size
int size()Return the total count of all elements. 返回所有元素的总计数。- Specified by:
sizein interfaceCollection<E>- Returns:
- total size | 总大小
-
contains
Check if multiset contains the element. 检查多重集是否包含元素。- Specified by:
containsin interfaceCollection<E>- Parameters:
element- the element | 元素- Returns:
- true if contains | 如果包含则返回 true
-
add
Add one occurrence of the element. 添加元素的一个出现。- Specified by:
addin interfaceCollection<E>- Parameters:
element- the element | 元素- Returns:
- always true | 始终为 true
-
remove
Remove one occurrence of the element. 移除元素的一个出现。- Specified by:
removein interfaceCollection<E>- Parameters:
element- the element | 元素- Returns:
- true if element was present | 如果元素存在则返回 true
-