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

public interface Multiset<E> extends Collection<E>
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:
  • Method Details

    • count

      int count(Object element)
      Return the count of the specified element. 返回指定元素的计数。
      Parameters:
      element - the element to count | 要计数的元素
      Returns:
      the count (0 if not present) | 计数(不存在则为 0)
    • add

      int add(E element, int occurrences)
      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

      int remove(Object element, int occurrences)
      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

      int setCount(E element, int count)
      Set the count of an element. 设置元素的计数。
      Parameters:
      element - the element | 元素
      count - the new count | 新计数
      Returns:
      previous count | 之前的计数
      Throws:
      IllegalArgumentException - if count is negative | 如果计数为负
    • setCount

      boolean setCount(E element, int oldCount, int newCount)
      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

      Set<E> elementSet()
      Return the set of distinct elements. 返回去重元素的集合。
      Returns:
      element set | 元素集合
    • 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:
      size in interface Collection<E>
      Returns:
      total size | 总大小
    • contains

      boolean contains(Object element)
      Check if multiset contains the element. 检查多重集是否包含元素。
      Specified by:
      contains in interface Collection<E>
      Parameters:
      element - the element | 元素
      Returns:
      true if contains | 如果包含则返回 true
    • add

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

      boolean remove(Object element)
      Remove one occurrence of the element. 移除元素的一个出现。
      Specified by:
      remove in interface Collection<E>
      Parameters:
      element - the element | 元素
      Returns:
      true if element was present | 如果元素存在则返回 true