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>

public class HashMultiset<E> extends AbstractCollection<E> implements Multiset<E>, Serializable
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:
  • Method Details

    • create

      public static <E> HashMultiset<E> create()
      Create an empty HashMultiset. 创建空 HashMultiset。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new HashMultiset | 新的 HashMultiset
    • create

      public static <E> HashMultiset<E> create(int initialCapacity)
      Create an empty HashMultiset with initial capacity. 创建指定容量的空 HashMultiset。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      initialCapacity - initial capacity | 初始容量
      Returns:
      new HashMultiset | 新的 HashMultiset
    • create

      public static <E> HashMultiset<E> create(Iterable<? extends E> elements)
      Create a HashMultiset from an iterable. 从可迭代对象创建 HashMultiset。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - elements | 元素
      Returns:
      new HashMultiset | 新的 HashMultiset
    • create

      @SafeVarargs public static <E> HashMultiset<E> create(E... elements)
      Create a HashMultiset from varargs. 从可变参数创建 HashMultiset。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - elements | 元素
      Returns:
      new HashMultiset | 新的 HashMultiset
    • 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 Set<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 | 条目集合
    • 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 element)
      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:
      element - the element | 元素
      Returns:
      true if contains | 如果包含则返回 true
    • add

      public boolean add(E element)
      Description copied from interface: Multiset
      Add one occurrence of the element. 添加元素的一个出现。
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Multiset<E>
      Overrides:
      add in class AbstractCollection<E>
      Parameters:
      element - the element | 元素
      Returns:
      always true | 始终为 true
    • remove

      public boolean remove(Object element)
      Description copied from interface: Multiset
      Remove one occurrence of the element. 移除元素的一个出现。
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Multiset<E>
      Overrides:
      remove in class AbstractCollection<E>
      Parameters:
      element - the element | 元素
      Returns:
      true if element was present | 如果元素存在则返回 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>
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Collection<E>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<E>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<E>