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
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:
-
Field Summary
Fields inherited from class AbstractMultimap
map -
Method Summary
Modifier and TypeMethodDescriptionMap<K, Collection<V>> asMap()Return a map view with key to collection mapping.Returns a view of this multimap as a map from keys to sorted sets of values.static <K, V extends Comparable<? super V>>
TreeSetMultimap<K, V> create()Create an empty TreeSetMultimap with natural ordering for values.static <K, V extends Comparable<? super V>>
TreeSetMultimap<K, V> Create a TreeSetMultimap from an existing multimap.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.static <K,V> TreeSetMultimap <K, V> create(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator) Create an empty TreeSetMultimap with sorted keys and values.static <K,V> TreeSetMultimap <K, V> create(Comparator<? super V> valueComparator) Create an empty TreeSetMultimap with a custom value comparator.protected Collection<V> Create a new collection for values.entries()Return a collection view of all key-value pairs.Returns the first (lowest) value for the given key.Return the collection of values for the key.Returns a view of values strictly less than the given value.Returns the last (highest) value for the given key.Remove all values for a key.replaceValues(K key, Iterable<? extends V> values) Replace all values for a key.Returns a view of values in the given range.Returns a view of values greater than or equal to the given value.Comparator<? super V> Returns the comparator used to order values in each key's collection.Methods inherited from class AbstractMultimap
clear, containsEntry, containsKey, containsValue, equals, hashCode, isEmpty, keys, keySet, put, putAll, remove, size, toString, valuesMethods inherited from interface Multimap
clear, containsEntry, containsKey, containsValue, isEmpty, keys, keySet, put, putAll, putAll, putAll, remove, size, values
-
Method Details
-
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
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
Description copied from class:AbstractMultimapCreate a new collection for values. 为值创建新集合。- Specified by:
createCollectionin classAbstractMultimap<K,V> - Returns:
- new collection | 新集合
-
get
Description copied from interface:MultimapReturn the collection of values for the key. 返回键的值集合。 -
removeAll
Description copied from interface:MultimapRemove all values for a key. 移除键的所有值。 -
replaceValues
Description copied from interface:MultimapReplace all values for a key. 替换键的所有值。- Specified by:
replaceValuesin interfaceMultimap<K,V> - Specified by:
replaceValuesin interfaceSetMultimap<K,V> - Specified by:
replaceValuesin interfaceSortedSetMultimap<K,V> - Overrides:
replaceValuesin classAbstractMultimap<K,V> - Parameters:
key- the key | 键values- the new values | 新值- Returns:
- previous values | 之前的值
-
entries
Description copied from interface:MultimapReturn a collection view of all key-value pairs. 返回所有键值对的集合视图。 -
valueComparator
Description copied from interface:SortedSetMultimapReturns the comparator used to order values in each key's collection. 返回用于对每个键的集合中的值进行排序的比较器。Returns null if natural ordering is used.
如果使用自然排序则返回 null。
- Specified by:
valueComparatorin interfaceSortedSetMultimap<K,V> - Returns:
- the comparator, or null if natural ordering | 比较器,如果使用自然排序则为 null
-
asMap
-
asMapOfSortedSets
Returns a view of this multimap as a map from keys to sorted sets of values. 返回此多值映射作为从键到排序集合值的映射的视图。- Specified by:
asMapOfSortedSetsin interfaceSortedSetMultimap<K,V> - Returns:
- a map view with SortedSet values | 具有 SortedSet 值的映射视图
-
first
-
last
-
headSet
-
tailSet
-
subSet
-