Class ImmutableMultiset<E>

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

public final class ImmutableMultiset<E> extends AbstractCollection<E> implements Serializable
ImmutableMultiset - Immutable Multiset (Bag) Implementation ImmutableMultiset - 不可变多重集(包)实现

A collection that supports order-independent equality and allows duplicate elements, tracking the count of each element. Cannot be modified after creation.

支持与顺序无关的相等性并允许重复元素的集合,跟踪每个元素的计数。创建后不能修改。

Features | 主要功能:

  • Immutable - 不可变
  • Thread-safe - 线程安全
  • Null-safe (nulls not allowed) - 空值安全(不允许空值)
  • Element counting - 元素计数
  • O(1) count operations - O(1) 计数操作

Usage Examples | 使用示例:

// Create from elements - 从元素创建
ImmutableMultiset<String> multiset = ImmutableMultiset.of("a", "b", "a", "c", "b", "a");
int count = multiset.count("a"); // Returns 3 - 返回 3

// Create from collection - 从集合创建
ImmutableMultiset<String> multiset = ImmutableMultiset.copyOf(Arrays.asList("x", "x", "y"));

// Use builder - 使用构建器
ImmutableMultiset<String> multiset = ImmutableMultiset.<String>builder()
    .add("a")
    .addAll(Arrays.asList("b", "c"))
    .setCount("a", 3)
    .build();

// Query operations - 查询操作
Set<String> elementSet = multiset.elementSet(); // Unique elements - 唯一元素
int totalSize = multiset.size(); // Total count of all elements - 所有元素的总数

Performance | 性能特性:

  • count: O(1) - count: O(1)
  • contains: O(1) - contains: O(1)
  • add/remove: Not supported (immutable) - add/remove: 不支持(不可变)
  • iteration: O(n) where n is total size - iteration: O(n),其中n是总大小

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Yes (nulls not allowed) - 空值安全: 是(不允许空值)
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static <E> ImmutableMultiset<E> of()
      Return an empty immutable multiset. 返回空不可变多重集。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      empty immutable multiset | 空不可变多重集
    • of

      public static <E> ImmutableMultiset<E> of(E e1)
      Return an immutable multiset containing the given element. 返回包含给定元素的不可变多重集。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      e1 - the element | 元素
      Returns:
      immutable multiset | 不可变多重集
    • of

      public static <E> ImmutableMultiset<E> of(E e1, E e2)
      Return an immutable multiset containing the given elements. 返回包含给定元素的不可变多重集。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      e1 - first element | 第一个元素
      e2 - second element | 第二个元素
      Returns:
      immutable multiset | 不可变多重集
    • of

      @SafeVarargs public static <E> ImmutableMultiset<E> of(E... elements)
      Return an immutable multiset containing the given elements. 返回包含给定元素的不可变多重集。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      immutable multiset | 不可变多重集
    • copyOf

      public static <E> ImmutableMultiset<E> copyOf(Collection<? extends E> elements)
      Return an immutable multiset containing the elements of the given collection. 返回包含给定集合元素的不可变多重集。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      immutable multiset | 不可变多重集
    • copyOf

      public static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> elements)
      Return an immutable multiset containing the elements of the given iterable. 返回包含给定可迭代对象元素的不可变多重集。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - the elements | 元素
      Returns:
      immutable multiset | 不可变多重集
    • builder

      public static <E> ImmutableMultiset.Builder<E> builder()
      Return a new builder. 返回新构建器。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      builder | 构建器
    • count

      public int count(Object element)
      Return the count of the given element in this multiset. 返回此多重集中给定元素的计数。
      Parameters:
      element - the element to count | 要计数的元素
      Returns:
      the count of the element, or 0 if not present | 元素的计数,如果不存在则为0
    • elementSet

      public Set<E> elementSet()
      Return the set of distinct elements in this multiset. 返回此多重集中的不同元素集。
      Returns:
      unmodifiable set of distinct elements | 不可修改的不同元素集
    • entrySet

      public Set<ImmutableMultiset.Entry<E>> entrySet()
      Return the set of entries with elements and their counts. 返回包含元素及其计数的条目集。
      Returns:
      unmodifiable set of entries | 不可修改的条目集
    • 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>
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in class AbstractCollection<E>
    • contains

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

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

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<E>
      Overrides:
      toArray in class AbstractCollection<E>
    • toArray

      public <T> T[] toArray(T[] a)
      Specified by:
      toArray in interface Collection<E>
      Overrides:
      toArray in class AbstractCollection<E>
    • add

      public boolean add(E e)
      Specified by:
      add in interface Collection<E>
      Overrides:
      add in class AbstractCollection<E>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<E>
      Overrides:
      remove in class AbstractCollection<E>
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Specified by:
      addAll in interface Collection<E>
      Overrides:
      addAll in class AbstractCollection<E>
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<E>
      Overrides:
      removeAll in class AbstractCollection<E>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<E>
      Overrides:
      retainAll 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>