Class LinkedHashMultimap<K,V>

java.lang.Object
cloud.opencode.base.collections.AbstractMultimap<K,V>
cloud.opencode.base.collections.specialized.LinkedHashMultimap<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型
All Implemented Interfaces:
Multimap<K,V>, Serializable

public final class LinkedHashMultimap<K,V> extends AbstractMultimap<K,V> implements Serializable
LinkedHashMultimap - Insertion-Ordered Multimap Implementation LinkedHashMultimap - 保持插入顺序的多重映射实现

A Multimap that preserves the insertion order of both keys and values. Uses LinkedHashMap for keys and LinkedHashSet for values, ensuring iteration order matches insertion order while preventing duplicate values per key.

保持键和值插入顺序的多重映射。使用 LinkedHashMap 存储键,LinkedHashSet 存储值, 确保迭代顺序与插入顺序一致,同时防止每个键有重复值。

Features | 主要功能:

  • Preserves insertion order of keys - 保持键的插入顺序
  • Preserves insertion order of values per key - 保持每个键的值的插入顺序
  • No duplicate values per key (Set semantics) - 每个键无重复值(集合语义)
  • O(1) put and contains operations - O(1) 放入和包含操作
  • Serializable - 可序列化

Usage Examples | 使用示例:

// Create empty - 创建空映射
LinkedHashMultimap<String, Integer> multimap = LinkedHashMultimap.create();

// Create with expected key count - 创建指定预期键数
LinkedHashMultimap<String, Integer> multimap = LinkedHashMultimap.create(16);

// Operations - 操作
multimap.put("b", 2);
multimap.put("a", 1);
multimap.put("a", 3);
multimap.put("a", 1);  // ignored (duplicate)

// Iteration preserves insertion order
// keySet: [b, a]
// get("a"): [1, 3]

Performance | 性能特性:

  • put: O(1) average - put: O(1) 平均
  • get: O(1) - get: O(1)
  • remove: O(1) average - remove: O(1) 平均
  • containsEntry: O(1) average - containsEntry: O(1) 平均
  • Iteration: insertion order - 迭代: 插入顺序

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Allows null keys and values - 空值安全: 允许空键和空值
Since:
JDK 25, opencode-base-collections V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static <K,V> LinkedHashMultimap<K,V> create()
      Create a new empty LinkedHashMultimap. 创建新的空 LinkedHashMultimap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new empty multimap | 新的空多重映射
    • create

      public static <K,V> LinkedHashMultimap<K,V> create(int expectedKeys)
      Create a new LinkedHashMultimap with expected key count. 创建指定预期键数的新 LinkedHashMultimap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      expectedKeys - expected number of keys | 预期键数
      Returns:
      new multimap | 新的多重映射
      Throws:
      IllegalArgumentException - if expectedKeys is negative | 如果预期键数为负
    • createCollection

      protected Collection<V> createCollection()
      Description copied from class: AbstractMultimap
      Create a new collection for values. 为值创建新集合。
      Specified by:
      createCollection in class AbstractMultimap<K,V>
      Returns:
      new collection | 新集合