Class ConcurrentHashMultiset<E>

java.lang.Object
java.util.AbstractCollection<E>
cloud.opencode.base.collections.specialized.ConcurrentHashMultiset<E>
Type Parameters:
E - element type | 元素类型
All Implemented Interfaces:
Multiset<E>, Serializable, Iterable<E>, Collection<E>

public final class ConcurrentHashMultiset<E> extends AbstractCollection<E> implements Multiset<E>, Serializable
ConcurrentHashMultiset - Thread-safe Multiset Implementation ConcurrentHashMultiset - 线程安全的多重集合实现

A thread-safe multiset implementation using ConcurrentHashMap.

使用 ConcurrentHashMap 的线程安全多重集合实现。

Features | 主要功能:

  • Thread-safe - 线程安全
  • High concurrency - 高并发
  • Lock-free reads - 无锁读取
  • Atomic updates - 原子更新

Usage Examples | 使用示例:

ConcurrentHashMultiset<String> multiset = ConcurrentHashMultiset.create();

// Thread-safe operations - 线程安全操作
multiset.add("apple", 3);
multiset.add("banana");

// Atomic count operations - 原子计数操作
int count = multiset.count("apple"); // 3

// Can be safely used from multiple threads - 可安全地从多个线程使用
ExecutorService executor = Executors.newFixedThreadPool(4);
for (int i = 0; i < 100; i++) {
    executor.submit(() -> multiset.add("concurrent"));
}

Performance | 性能特性:

  • add: O(1) average - add: O(1) 平均
  • remove: O(1) average - remove: O(1) 平均
  • count: O(1) - count: O(1)
  • contains: O(1) - contains: O(1)

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • 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> ConcurrentHashMultiset<E> create()
      Create an empty ConcurrentHashMultiset. 创建空 ConcurrentHashMultiset。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new empty ConcurrentHashMultiset | 新空 ConcurrentHashMultiset
    • create

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

      public static <E> ConcurrentHashMultiset<E> create(Iterable<? extends E> elements)
      Create a ConcurrentHashMultiset from elements. 从元素创建 ConcurrentHashMultiset。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      new ConcurrentHashMultiset | 新 ConcurrentHashMultiset
    • 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 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
    • 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>