Class LinkedHashMultiset<E>

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

public final class LinkedHashMultiset<E> extends AbstractMultiset<E> implements Serializable
LinkedHashMultiset - Linked Hash Multiset Implementation LinkedHashMultiset - 链式哈希多重集合实现

A multiset that maintains insertion order of elements.

保持元素插入顺序的多重集合。

Features | 主要功能:

  • Insertion order preservation - 保持插入顺序
  • O(1) operations - O(1) 操作
  • Predictable iteration order - 可预测的迭代顺序

Usage Examples | 使用示例:

LinkedHashMultiset<String> multiset = LinkedHashMultiset.create();
multiset.add("cherry", 3);
multiset.add("apple", 2);
multiset.add("banana");

// Iteration in insertion order - 按插入顺序迭代
for (String element : multiset.elementSet()) {
    System.out.println(element + ": " + multiset.count(element));
}
// Output: cherry: 3, apple: 2, banana: 1

Performance | 性能特性:

  • add: O(1) - add: O(1)
  • remove: O(1) - remove: O(1)
  • count: O(1) - count: O(1)
  • contains: O(1) - contains: O(1)

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> LinkedHashMultiset<E> create()
      Create an empty LinkedHashMultiset. 创建空 LinkedHashMultiset。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new empty LinkedHashMultiset | 新空 LinkedHashMultiset
    • create

      public static <E> LinkedHashMultiset<E> create(int expectedDistinctElements)
      Create a LinkedHashMultiset with expected size. 创建指定预期大小的 LinkedHashMultiset。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      expectedDistinctElements - expected number of distinct elements | 预期不同元素数量
      Returns:
      new empty LinkedHashMultiset | 新空 LinkedHashMultiset
    • create

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