Class TreeSetMultimap<K,V>

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

public class TreeSetMultimap<K,V> extends AbstractMultimap<K,V> implements SortedSetMultimap<K,V>
TreeSetMultimap - SortedSetMultimap Implementation using TreeSet TreeSetMultimap - 使用 TreeSet 的 SortedSetMultimap 实现

A Multimap implementation that uses TreeSet for each key's values, maintaining elements in sorted order. Duplicate values per key are not allowed.

使用 TreeSet 存储每个键的值的 Multimap 实现,保持元素的排序顺序。 不允许每个键有重复值。

Features | 主要功能:

  • Sorted values per key - 每个键的值有序
  • No duplicate values - 无重复值
  • Natural or custom ordering - 自然排序或自定义排序
  • Serializable - 可序列化

Usage Examples | 使用示例:

// Create with natural ordering
TreeSetMultimap<String, Integer> multimap = TreeSetMultimap.create();
multimap.put("numbers", 3);
multimap.put("numbers", 1);
multimap.put("numbers", 2);
SortedSet<Integer> values = multimap.get("numbers"); // [1, 2, 3]

// Create with custom comparator (reverse order)
TreeSetMultimap<String, Integer> reverse = TreeSetMultimap.create(
    Comparator.reverseOrder());
reverse.put("numbers", 3);
reverse.put("numbers", 1);
reverse.put("numbers", 2);
SortedSet<Integer> reverseValues = reverse.get("numbers"); // [3, 2, 1]

// Create with sorted keys and values
TreeSetMultimap<String, Integer> sortedBoth = TreeSetMultimap.create(
    String.CASE_INSENSITIVE_ORDER,
    Comparator.naturalOrder());

// Get first/last values
Integer first = multimap.get("numbers").first(); // 1
Integer last = multimap.get("numbers").last();   // 3

// Subset operations
SortedSet<Integer> headSet = multimap.get("numbers").headSet(2); // [1]
SortedSet<Integer> tailSet = multimap.get("numbers").tailSet(2); // [2, 3]
SortedSet<Integer> subSet = multimap.get("numbers").subSet(1, 3); // [1, 2]

Performance | 性能特性:

  • put: O(log n) - put: O(log n)
  • get: O(1) for key lookup - get: O(1) 键查找
  • contains: O(log n) - contains: O(log n)
  • remove: O(log n) - remove: O(log n)
  • first/last: O(log n) - first/last: O(log n)

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Depends on comparator - 空值安全: 取决于比较器
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static <K, V extends Comparable<? super V>> TreeSetMultimap<K,V> create()
      Create an empty TreeSetMultimap with natural ordering for values. 创建具有值自然排序的空 TreeSetMultimap。
      Type Parameters:
      K - key type | 键类型
      V - value type (must be Comparable) | 值类型(必须是 Comparable)
      Returns:
      new TreeSetMultimap | 新的 TreeSetMultimap
    • create

      public static <K,V> TreeSetMultimap<K,V> create(Comparator<? super V> valueComparator)
      Create an empty TreeSetMultimap with a custom value comparator. 创建具有自定义值比较器的空 TreeSetMultimap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      valueComparator - the comparator for values | 值的比较器
      Returns:
      new TreeSetMultimap | 新的 TreeSetMultimap
    • create

      public static <K,V> TreeSetMultimap<K,V> create(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator)
      Create an empty TreeSetMultimap with sorted keys and values. 创建具有排序键和值的空 TreeSetMultimap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      keyComparator - the comparator for keys (null for natural order) | 键的比较器(null 表示自然顺序)
      valueComparator - the comparator for values (null for natural order) | 值的比较器(null 表示自然顺序)
      Returns:
      new TreeSetMultimap | 新的 TreeSetMultimap
    • create

      public static <K, V extends Comparable<? super V>> TreeSetMultimap<K,V> create(Multimap<? extends K, ? extends V> multimap)
      Create a TreeSetMultimap from an existing multimap. 从现有多重映射创建 TreeSetMultimap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      multimap - source multimap | 源多重映射
      Returns:
      new TreeSetMultimap | 新的 TreeSetMultimap
    • create

      public static <K,V> TreeSetMultimap<K,V> create(Multimap<? extends K, ? extends V> multimap, Comparator<? super V> valueComparator)
      Create a TreeSetMultimap from an existing multimap with custom comparator. 从现有多重映射创建具有自定义比较器的 TreeSetMultimap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      multimap - source multimap | 源多重映射
      valueComparator - the comparator for values | 值的比较器
      Returns:
      new TreeSetMultimap | 新的 TreeSetMultimap
    • 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 | 新集合
    • get

      public SortedSet<V> get(K key)
      Description copied from interface: Multimap
      Return the collection of values for the key. 返回键的值集合。
      Specified by:
      get in interface Multimap<K,V>
      Specified by:
      get in interface SetMultimap<K,V>
      Specified by:
      get in interface SortedSetMultimap<K,V>
      Overrides:
      get in class AbstractMultimap<K,V>
      Parameters:
      key - the key | 键
      Returns:
      values collection (never null, may be empty) | 值集合(不为 null,可能为空)
    • removeAll

      public SortedSet<V> removeAll(Object key)
      Description copied from interface: Multimap
      Remove all values for a key. 移除键的所有值。
      Specified by:
      removeAll in interface Multimap<K,V>
      Specified by:
      removeAll in interface SetMultimap<K,V>
      Specified by:
      removeAll in interface SortedSetMultimap<K,V>
      Overrides:
      removeAll in class AbstractMultimap<K,V>
      Parameters:
      key - the key | 键
      Returns:
      removed values | 移除的值
    • replaceValues

      public SortedSet<V> replaceValues(K key, Iterable<? extends V> values)
      Description copied from interface: Multimap
      Replace all values for a key. 替换键的所有值。
      Specified by:
      replaceValues in interface Multimap<K,V>
      Specified by:
      replaceValues in interface SetMultimap<K,V>
      Specified by:
      replaceValues in interface SortedSetMultimap<K,V>
      Overrides:
      replaceValues in class AbstractMultimap<K,V>
      Parameters:
      key - the key | 键
      values - the new values | 新值
      Returns:
      previous values | 之前的值
    • entries

      public Set<Map.Entry<K,V>> entries()
      Description copied from interface: Multimap
      Return a collection view of all key-value pairs. 返回所有键值对的集合视图。
      Specified by:
      entries in interface Multimap<K,V>
      Specified by:
      entries in interface SetMultimap<K,V>
      Overrides:
      entries in class AbstractMultimap<K,V>
      Returns:
      entries collection | 条目集合
    • valueComparator

      public Comparator<? super V> valueComparator()
      Description copied from interface: SortedSetMultimap
      Returns the comparator used to order values in each key's collection. 返回用于对每个键的集合中的值进行排序的比较器。

      Returns null if natural ordering is used.

      如果使用自然排序则返回 null。

      Specified by:
      valueComparator in interface SortedSetMultimap<K,V>
      Returns:
      the comparator, or null if natural ordering | 比较器,如果使用自然排序则为 null
    • asMap

      public Map<K, Collection<V>> asMap()
      Description copied from interface: Multimap
      Return a map view with key to collection mapping. 返回键到集合映射的视图。
      Specified by:
      asMap in interface Multimap<K,V>
      Overrides:
      asMap in class AbstractMultimap<K,V>
      Returns:
      map view | 映射视图
    • asMapOfSortedSets

      public Map<K, SortedSet<V>> asMapOfSortedSets()
      Returns a view of this multimap as a map from keys to sorted sets of values. 返回此多值映射作为从键到排序集合值的映射的视图。
      Specified by:
      asMapOfSortedSets in interface SortedSetMultimap<K,V>
      Returns:
      a map view with SortedSet values | 具有 SortedSet 值的映射视图
    • first

      public Optional<V> first(K key)
      Returns the first (lowest) value for the given key. 返回给定键的第一个(最小)值。
      Parameters:
      key - the key | 键
      Returns:
      the first value, or empty if no values | 第一个值,如果没有值则为空
    • last

      public Optional<V> last(K key)
      Returns the last (highest) value for the given key. 返回给定键的最后一个(最大)值。
      Parameters:
      key - the key | 键
      Returns:
      the last value, or empty if no values | 最后一个值,如果没有值则为空
    • headSet

      public SortedSet<V> headSet(K key, V toValue)
      Returns a view of values strictly less than the given value. 返回严格小于给定值的值视图。
      Parameters:
      key - the key | 键
      toValue - high endpoint (exclusive) | 上限端点(不包含)
      Returns:
      the head set | 头集合
    • tailSet

      public SortedSet<V> tailSet(K key, V fromValue)
      Returns a view of values greater than or equal to the given value. 返回大于或等于给定值的值视图。
      Parameters:
      key - the key | 键
      fromValue - low endpoint (inclusive) | 下限端点(包含)
      Returns:
      the tail set | 尾集合
    • subSet

      public SortedSet<V> subSet(K key, V fromValue, V toValue)
      Returns a view of values in the given range. 返回给定范围内的值视图。
      Parameters:
      key - the key | 键
      fromValue - low endpoint (inclusive) | 下限端点(包含)
      toValue - high endpoint (exclusive) | 上限端点(不包含)
      Returns:
      the sub set | 子集合