Class TreeMultiset<E>
java.lang.Object
java.util.AbstractCollection<E>
cloud.opencode.base.collections.specialized.AbstractMultiset<E>
cloud.opencode.base.collections.specialized.TreeMultiset<E>
- Type Parameters:
E- element type | 元素类型
- All Implemented Interfaces:
Multiset<E>, NavigableMultiset<E>, Serializable, Iterable<E>, Collection<E>
public final class TreeMultiset<E>
extends AbstractMultiset<E>
implements NavigableMultiset<E>, Serializable
TreeMultiset - Tree-based Multiset Implementation
TreeMultiset - 基于树的多重集合实现
A multiset that stores elements in sorted order using a tree structure.
使用树结构按排序顺序存储元素的多重集合。
Features | 主要功能:
- Sorted elements - 排序元素
- Natural or custom ordering - 自然或自定义排序
- NavigableSet view - 可导航集合视图
- O(log n) operations - O(log n) 操作
Usage Examples | 使用示例:
// Natural ordering - 自然排序
TreeMultiset<String> multiset = TreeMultiset.create();
multiset.add("banana", 2);
multiset.add("apple", 3);
multiset.add("cherry");
// Iteration in sorted order - 按排序顺序迭代
for (String element : multiset.elementSet()) {
System.out.println(element + ": " + multiset.count(element));
}
// Output: apple: 3, banana: 2, cherry: 1
// Custom comparator - 自定义比较器
TreeMultiset<String> reverseMultiset = TreeMultiset.create(Comparator.reverseOrder());
Performance | 性能特性:
- add: O(log n) - add: O(log n)
- remove: O(log n) - remove: O(log n)
- count: O(log n) - count: O(log n)
- contains: O(log n) - contains: O(log n)
Security | 安全性:
- Thread-safe: No - 线程安全: 否
- 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 TypeMethodDescriptionintAdd a number of occurrences of an element.Return the least element greater than or equal to the given element.voidclear()Comparator<? super E> Return the comparator used to order elements.booleanCheck if multiset contains the element.intReturn the count of the specified element.static <E extends Comparable<? super E>>
TreeMultiset<E> create()Create an empty TreeMultiset with natural ordering.static <E extends Comparable<? super E>>
TreeMultiset<E> Create a TreeMultiset from elements with natural ordering.static <E> TreeMultiset<E> create(Comparator<? super E> comparator) Create an empty TreeMultiset with custom comparator.Return the set of distinct elements.entrySet()Return the set of entries (element with count).first()Return the first (lowest) element.Return the greatest element less than or equal to the given element.Return the least element strictly greater than the given element.booleanisEmpty()iterator()last()Return the last (highest) element.Return the greatest element strictly less than the given element.Remove and return the first entry, or null if empty.Remove and return the last entry, or null if empty.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 class AbstractMultiset
add, addAll, containsAll, equals, hashCode, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from interface Collection
addAll, containsAll, equals, hashCode, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Method Details
-
create
Create an empty TreeMultiset with natural ordering. 创建自然排序的空 TreeMultiset。- Type Parameters:
E- element type | 元素类型- Returns:
- new empty TreeMultiset | 新空 TreeMultiset
-
create
Create an empty TreeMultiset with custom comparator. 创建自定义比较器的空 TreeMultiset。- Type Parameters:
E- element type | 元素类型- Parameters:
comparator- the comparator | 比较器- Returns:
- new empty TreeMultiset | 新空 TreeMultiset
-
create
public static <E extends Comparable<? super E>> TreeMultiset<E> create(Iterable<? extends E> elements) Create a TreeMultiset from elements with natural ordering. 从元素创建自然排序的 TreeMultiset。- Type Parameters:
E- element type | 元素类型- Parameters:
elements- the elements | 元素- Returns:
- new TreeMultiset | 新 TreeMultiset
-
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
Description copied from interface:MultisetReturn the set of distinct elements. 返回去重元素的集合。- Specified by:
elementSetin interfaceMultiset<E>- Returns:
- element set | 元素集合
-
entrySet
-
first
Description copied from interface:NavigableMultisetReturn the first (lowest) element. 返回第一个(最小)元素。- Specified by:
firstin interfaceNavigableMultiset<E>- Returns:
- the first element | 第一个元素
-
last
Description copied from interface:NavigableMultisetReturn the last (highest) element. 返回最后一个(最大)元素。- Specified by:
lastin interfaceNavigableMultiset<E>- Returns:
- the last element | 最后一个元素
-
lower
Description copied from interface:NavigableMultisetReturn the greatest element strictly less than the given element. 返回严格小于给定元素的最大元素。- Specified by:
lowerin interfaceNavigableMultiset<E>- Parameters:
e- the element | 元素- Returns:
- the lower element, or null | 更小的元素,或 null
-
higher
Description copied from interface:NavigableMultisetReturn the least element strictly greater than the given element. 返回严格大于给定元素的最小元素。- Specified by:
higherin interfaceNavigableMultiset<E>- Parameters:
e- the element | 元素- Returns:
- the higher element, or null | 更大的元素,或 null
-
floor
Description copied from interface:NavigableMultisetReturn the greatest element less than or equal to the given element. 返回小于或等于给定元素的最大元素。- Specified by:
floorin interfaceNavigableMultiset<E>- Parameters:
e- the element | 元素- Returns:
- the floor element, or null | floor 元素,或 null
-
ceiling
Description copied from interface:NavigableMultisetReturn the least element greater than or equal to the given element. 返回大于或等于给定元素的最小元素。- Specified by:
ceilingin interfaceNavigableMultiset<E>- Parameters:
e- the element | 元素- Returns:
- the ceiling element, or null | ceiling 元素,或 null
-
pollFirstEntry
Description copied from interface:NavigableMultisetRemove and return the first entry, or null if empty. 移除并返回第一个条目,如果为空则返回 null。- Specified by:
pollFirstEntryin interfaceNavigableMultiset<E>- Returns:
- the first entry, or null | 第一个条目,或 null
-
pollLastEntry
Description copied from interface:NavigableMultisetRemove and return the last entry, or null if empty. 移除并返回最后一个条目,如果为空则返回 null。- Specified by:
pollLastEntryin interfaceNavigableMultiset<E>- Returns:
- the last entry, or null | 最后一个条目,或 null
-
comparator
Description copied from interface:NavigableMultisetReturn the comparator used to order elements. 返回用于排序元素的比较器。- Specified by:
comparatorin interfaceNavigableMultiset<E>- Returns:
- the comparator, or null for natural ordering | 比较器,自然排序则为 null
-
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
-
iterator
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
-