java.lang.Object
java.util.AbstractMap<K,V>
org.pcollections.AbstractUnmodifiableMap<K,V>
org.pcollections.TreePMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
Serializable,Map<K,,V> NavigableMap<K,,V> SortedMap<K,,V> PMap<K,,V> PSortedMap<K,V>
public final class TreePMap<K,V>
extends AbstractUnmodifiableMap<K,V>
implements PSortedMap<K,V>, Serializable
An implementation of
PSortedMap based on a self-balancing binary search tree.
Instances of this class are obtained via any of various static factory methods and static collector methods. These methods come in pairs, with one version that accepts an explicit comparator to use and one version that uses the natural ordering of the elements.
All operations are guaranteed to complete within O(log n) time, except for plusAll and minusAll, whose time cost is equivalent to the corresponding sequence of calls to plus or minus. A complete iteration pass over entrySet() completes in O(n) time. A few operations -- namely comparator, descendingKeySet, descendingMap, entrySet, isEmpty, keySet, navigableKeySet, and size -- complete in O(1) time.
- Since:
- 3.2.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Method Summary
Modifier and TypeMethodDescriptionceilingEntry(K key) ceilingKey(K key) Comparator<? super K>booleancontainsKey(Object key) static <K extends Comparable<? super K>,V>
TreePMap<K,V> empty()Returns an empty TreePMap using the natural ordering.static <K,V> TreePMap<K, V> empty(Comparator<? super K> comparator) Returns an empty TreePMap using the specified comparator.entrySet()firstKey()floorEntry(K key) static <K,V> TreePMap<K, V> from(Comparator<? super K> comparator, Map<? extends K, ? extends V> map) Returns a TreePMap with the specified comparator and mappings.static <K extends Comparable<? super K>,V>
TreePMap<K,V> Returns a TreePMap with the specified mappings, using the natural ordering of the keys.static <K,V> TreePMap<K, V> fromSortedMap(SortedMap<K, ? extends V> map) Returns a TreePMap with the same mappings and ordering as the specified map.higherEntry(K key) booleanisEmpty()keySet()lastKey()lowerEntry(K key) minusAll(Collection<?> keys) static <K,V> TreePMap<K, V> singleton(Comparator<? super K> comparator, K key, V value) Returns a TreePMap with a single element, using the specified comparator.static <K extends Comparable<? super K>,V>
TreePMap<K,V> singleton(K key, V value) Returns a TreePMap with a single mapping, using the natural ordering of its keys.intsize()toTreePMap(Comparator<? super K> comparator, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Returns a collector that gathers a stream into a TreePMap with mappings computed from the elements of the stream (using the specified keyMapper and valueMapper), in the order determined by the specified comparator.toTreePMap(Comparator<? super K> comparator, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergeFunction) Returns a collector that gathers a stream into a TreePMap with mappings computed from the elements of the stream (using the specified keyMapper and valueMapper, with duplicates reconciled via the specified mergeFunction), in the order determined by the specified comparator.static <T,K extends Comparable<? super K>, V>
Collector<T,?, TreePMap<K, V>> toTreePMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Returns a collector that gathers a stream into a TreePMap with mappings computed from the elements of the stream (using the specified keyMapper and valueMapper), in the order determined by the natural ordering of the keys.static <T,K extends Comparable<? super K>, V>
Collector<T,?, TreePMap<K, V>> toTreePMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergeFunction) Returns a collector that gathers a stream into a TreePMap with mappings computed from the elements of the stream (using the specified keyMapper and valueMapper, with duplicates reconciled via the specified mergeFunction), in the order determined by the natural ordering of the keys.Methods inherited from class org.pcollections.AbstractUnmodifiableMap
clear, compute, computeIfAbsent, computeIfPresent, merge, put, putAll, putIfAbsent, remove, replace, replaceAllMethods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, toString, valuesMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsValue, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAllMethods inherited from interface org.pcollections.PSortedMap
pollFirstEntry, pollLastEntry
-
Method Details
-
empty
Returns an empty TreePMap using the natural ordering.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Returns:
- an empty TreePMap using the natural ordering
-
empty
Returns an empty TreePMap using the specified comparator.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
comparator- the comparator according to which keys should be ordered- Returns:
- an empty TreePMap using the specified comparator
- Throws:
NullPointerException- if comparator is null
-
from
public static <K extends Comparable<? super K>,V> TreePMap<K,V> from(Map<? extends K, ? extends V> map) Returns a TreePMap with the specified mappings, using the natural ordering of the keys.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
map- the mappings to include- Returns:
- a TreePMap containing the specified mappings and using the natural ordering of the keys
- Throws:
NullPointerException- if the map is null or contains a null key
-
from
public static <K,V> TreePMap<K,V> from(Comparator<? super K> comparator, Map<? extends K, ? extends V> map) Returns a TreePMap with the specified comparator and mappings.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
comparator- the comparator to usemap- the mappings to include- Returns:
- a TreePMap with the specified comparator and mappings
- Throws:
NullPointerException- if the comparator or map is null or the map contains a null key
-
fromSortedMap
Returns a TreePMap with the same mappings and ordering as the specified map. This is essentially equivalent toTreePMap.from(map.comparator(), map), except that it gracefully handles a null comparator, and is much more efficient.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
map- the map whose mappings and ordering to use- Returns:
- a TreePMap with the same mappings and ordering as the specified set
- Throws:
NullPointerException- if the specified map is null or contains a null key
-
singleton
Returns a TreePMap with a single mapping, using the natural ordering of its keys.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
key- the keyvalue- the value- Returns:
- a TreePMap containing the specified mapping and using the natural ordering
- Throws:
NullPointerException- if the specified key or value is null
-
singleton
Returns a TreePMap with a single element, using the specified comparator.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
comparator- the comparator according to which keys should be orderedkey- the keyvalue- the value- Returns:
- a TreePMap containing the specified mapping and using the specified comparator
- Throws:
NullPointerException- if the specified comparator or key is null
-
toTreePMap
public static <T,K extends Comparable<? super K>, Collector<T,V> ?, toTreePMapTreePMap<K, V>> (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Returns a collector that gathers a stream into a TreePMap with mappings computed from the elements of the stream (using the specified keyMapper and valueMapper), in the order determined by the natural ordering of the keys. In the event of duplicate keys, the collector will throw IllegalStateException.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
keyMapper- a function to compute the key from a stream elementvalueMapper- a function to compute the value from a stream element- Returns:
- a collector that gathers the elements of the stream into a TreePMap
- Throws:
NullPointerException- if either keyMapper or valueMapper is null
-
toTreePMap
public static <T,K, Collector<T,V> ?, toTreePMapTreePMap<K, V>> (Comparator<? super K> comparator, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Returns a collector that gathers a stream into a TreePMap with mappings computed from the elements of the stream (using the specified keyMapper and valueMapper), in the order determined by the specified comparator. In the event of duplicate keys, the collector will throw IllegalStateException.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
comparator- the comparator according to which keys should be orderedkeyMapper- a function to compute the key from a stream elementvalueMapper- a function to compute the value from a stream element- Returns:
- a collector that gathers the elements of the stream into a TreePMap
- Throws:
NullPointerException- if any of this method's arguments are null
-
toTreePMap
public static <T,K extends Comparable<? super K>, Collector<T,V> ?, toTreePMapTreePMap<K, V>> (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergeFunction) Returns a collector that gathers a stream into a TreePMap with mappings computed from the elements of the stream (using the specified keyMapper and valueMapper, with duplicates reconciled via the specified mergeFunction), in the order determined by the natural ordering of the keys.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
keyMapper- a function to compute the key from a stream elementvalueMapper- a function to compute the value from a stream elementmergeFunction- a function to merge duplicate values- Returns:
- a collector that gathers the elements of the stream into a TreePMap
- Throws:
NullPointerException- if any of this method's arguments are null
-
toTreePMap
public static <T,K, Collector<T,V> ?, toTreePMapTreePMap<K, V>> (Comparator<? super K> comparator, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergeFunction) Returns a collector that gathers a stream into a TreePMap with mappings computed from the elements of the stream (using the specified keyMapper and valueMapper, with duplicates reconciled via the specified mergeFunction), in the order determined by the specified comparator.- Type Parameters:
K- the type of keys to be maintained by the mapV- the type of mapped values- Parameters:
comparator- the comparator according to which keys should be orderedkeyMapper- a function to compute the key from a stream elementvalueMapper- a function to compute the value from a stream elementmergeFunction- a function to merge duplicate values- Returns:
- a collector that gathers the elements of the stream into a TreePMap
- Throws:
NullPointerException- if any of this method's arguments are null
-
ceilingEntry
- Specified by:
ceilingEntryin interfaceNavigableMap<K,V>
-
ceilingKey
- Specified by:
ceilingKeyin interfaceNavigableMap<K,V>
-
comparator
- Specified by:
comparatorin interfacePSortedMap<K,V> - Specified by:
comparatorin interfaceSortedMap<K,V> - Returns:
- The comparator used to order the keys in this map. (Never null.)
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-
descendingKeySet
- Specified by:
descendingKeySetin interfaceNavigableMap<K,V> - Specified by:
descendingKeySetin interfacePSortedMap<K,V>
-
descendingMap
- Specified by:
descendingMapin interfaceNavigableMap<K,V> - Specified by:
descendingMapin interfacePSortedMap<K,V>
-
entrySet
-
firstEntry
- Specified by:
firstEntryin interfaceNavigableMap<K,V>
-
firstKey
-
floorEntry
- Specified by:
floorEntryin interfaceNavigableMap<K,V>
-
floorKey
- Specified by:
floorKeyin interfaceNavigableMap<K,V>
-
get
-
headMap
-
headMap
- Specified by:
headMapin interfaceNavigableMap<K,V> - Specified by:
headMapin interfacePSortedMap<K,V>
-
higherEntry
- Specified by:
higherEntryin interfaceNavigableMap<K,V>
-
higherKey
- Specified by:
higherKeyin interfaceNavigableMap<K,V>
-
isEmpty
public boolean isEmpty() -
keySet
-
lastEntry
- Specified by:
lastEntryin interfaceNavigableMap<K,V>
-
lastKey
-
lowerEntry
- Specified by:
lowerEntryin interfaceNavigableMap<K,V>
-
lowerKey
- Specified by:
lowerKeyin interfaceNavigableMap<K,V>
-
minus
-
minusAll
-
minusFirstEntry
- Specified by:
minusFirstEntryin interfacePSortedMap<K,V> - Returns:
- This map, minus its first mapping (the mapping with the least/lowest key).
-
minusLastEntry
- Specified by:
minusLastEntryin interfacePSortedMap<K,V> - Returns:
- This map, minus its last mapping (the mapping with the greatest/highest key).
-
plus
-
plusAll
-
size
public int size() -
subMap
-
subMap
- Specified by:
subMapin interfaceNavigableMap<K,V> - Specified by:
subMapin interfacePSortedMap<K,V>
-
tailMap
-
tailMap
- Specified by:
tailMapin interfaceNavigableMap<K,V> - Specified by:
tailMapin interfacePSortedMap<K,V>
-