Class 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:
  • Method Details

    • create

      public static <E extends Comparable<? super E>> TreeMultiset<E> create()
      Create an empty TreeMultiset with natural ordering. 创建自然排序的空 TreeMultiset。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new empty TreeMultiset | 新空 TreeMultiset
    • create

      public static <E> TreeMultiset<E> create(Comparator<? super E> comparator)
      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

      public int count(Object element)
      Description copied from interface: Multiset
      Return the count of the specified element. 返回指定元素的计数。
      Specified by:
      count in interface Multiset<E>
      Parameters:
      element - the element to count | 要计数的元素
      Returns:
      the count (0 if not present) | 计数(不存在则为 0)
    • add

      public int add(E element, int occurrences)
      Description copied from interface: Multiset
      Add a number of occurrences of an element. 添加元素的多个出现。
      Specified by:
      add in interface Multiset<E>
      Parameters:
      element - the element to add | 要添加的元素
      occurrences - number of occurrences to add | 要添加的出现次数
      Returns:
      previous count | 之前的计数
    • remove

      public int remove(Object element, int occurrences)
      Description copied from interface: Multiset
      Remove a number of occurrences of an element. 移除元素的多个出现。
      Specified by:
      remove in interface Multiset<E>
      Parameters:
      element - the element to remove | 要移除的元素
      occurrences - number of occurrences to remove | 要移除的出现次数
      Returns:
      previous count | 之前的计数
    • setCount

      public int setCount(E element, int count)
      Description copied from interface: Multiset
      Set the count of an element. 设置元素的计数。
      Specified by:
      setCount in interface Multiset<E>
      Parameters:
      element - the element | 元素
      count - the new count | 新计数
      Returns:
      previous count | 之前的计数
    • setCount

      public boolean setCount(E element, int oldCount, int newCount)
      Description copied from interface: Multiset
      Conditionally set the count of an element. 条件性地设置元素的计数。
      Specified by:
      setCount in interface Multiset<E>
      Parameters:
      element - the element | 元素
      oldCount - expected current count | 期望的当前计数
      newCount - the new count | 新计数
      Returns:
      true if count was changed | 如果计数被更改则返回 true
    • elementSet

      public NavigableSet<E> elementSet()
      Description copied from interface: Multiset
      Return the set of distinct elements. 返回去重元素的集合。
      Specified by:
      elementSet in interface Multiset<E>
      Returns:
      element set | 元素集合
    • entrySet

      public Set<Multiset.Entry<E>> entrySet()
      Description copied from interface: Multiset
      Return the set of entries (element with count). 返回条目集合(带计数的元素)。
      Specified by:
      entrySet in interface Multiset<E>
      Returns:
      entry set | 条目集合
    • first

      public E first()
      Description copied from interface: NavigableMultiset
      Return the first (lowest) element. 返回第一个(最小)元素。
      Specified by:
      first in interface NavigableMultiset<E>
      Returns:
      the first element | 第一个元素
    • last

      public E last()
      Description copied from interface: NavigableMultiset
      Return the last (highest) element. 返回最后一个(最大)元素。
      Specified by:
      last in interface NavigableMultiset<E>
      Returns:
      the last element | 最后一个元素
    • lower

      public E lower(E e)
      Description copied from interface: NavigableMultiset
      Return the greatest element strictly less than the given element. 返回严格小于给定元素的最大元素。
      Specified by:
      lower in interface NavigableMultiset<E>
      Parameters:
      e - the element | 元素
      Returns:
      the lower element, or null | 更小的元素,或 null
    • higher

      public E higher(E e)
      Description copied from interface: NavigableMultiset
      Return the least element strictly greater than the given element. 返回严格大于给定元素的最小元素。
      Specified by:
      higher in interface NavigableMultiset<E>
      Parameters:
      e - the element | 元素
      Returns:
      the higher element, or null | 更大的元素,或 null
    • floor

      public E floor(E e)
      Description copied from interface: NavigableMultiset
      Return the greatest element less than or equal to the given element. 返回小于或等于给定元素的最大元素。
      Specified by:
      floor in interface NavigableMultiset<E>
      Parameters:
      e - the element | 元素
      Returns:
      the floor element, or null | floor 元素,或 null
    • ceiling

      public E ceiling(E e)
      Description copied from interface: NavigableMultiset
      Return the least element greater than or equal to the given element. 返回大于或等于给定元素的最小元素。
      Specified by:
      ceiling in interface NavigableMultiset<E>
      Parameters:
      e - the element | 元素
      Returns:
      the ceiling element, or null | ceiling 元素,或 null
    • pollFirstEntry

      public Multiset.Entry<E> pollFirstEntry()
      Description copied from interface: NavigableMultiset
      Remove and return the first entry, or null if empty. 移除并返回第一个条目,如果为空则返回 null。
      Specified by:
      pollFirstEntry in interface NavigableMultiset<E>
      Returns:
      the first entry, or null | 第一个条目,或 null
    • pollLastEntry

      public Multiset.Entry<E> pollLastEntry()
      Description copied from interface: NavigableMultiset
      Remove and return the last entry, or null if empty. 移除并返回最后一个条目,如果为空则返回 null。
      Specified by:
      pollLastEntry in interface NavigableMultiset<E>
      Returns:
      the last entry, or null | 最后一个条目,或 null
    • comparator

      public Comparator<? super E> comparator()
      Description copied from interface: NavigableMultiset
      Return the comparator used to order elements. 返回用于排序元素的比较器。
      Specified by:
      comparator in interface NavigableMultiset<E>
      Returns:
      the comparator, or null for natural ordering | 比较器,自然排序则为 null
    • size

      public int size()
      Description copied from interface: Multiset
      Return the total count of all elements. 返回所有元素的总计数。
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Multiset<E>
      Specified by:
      size in class AbstractCollection<E>
      Returns:
      total size | 总大小
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
    • contains

      public boolean contains(Object o)
      Description copied from interface: Multiset
      Check if multiset contains the element. 检查多重集是否包含元素。
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Multiset<E>
      Overrides:
      contains in class AbstractCollection<E>
      Parameters:
      o - the element | 元素
      Returns:
      true if contains | 如果包含则返回 true
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in class AbstractCollection<E>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Overrides:
      clear in class AbstractCollection<E>