Package org.pcollections
Class TreePMap<K,V>
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>
boolean
containsKey
(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) boolean
isEmpty()
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.int
size()
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, replaceAll
Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, toString, values
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsValue, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
Methods 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 or value
-
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 or value
-
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 or value
-
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, key, or value 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:
ceilingEntry
in interfaceNavigableMap<K,
V>
-
ceilingKey
- Specified by:
ceilingKey
in interfaceNavigableMap<K,
V>
-
comparator
- Specified by:
comparator
in interfacePSortedMap<K,
V> - Specified by:
comparator
in interfaceSortedMap<K,
V> - Returns:
- The comparator used to order the keys in this map. (Never null.)
-
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Overrides:
containsKey
in classAbstractMap<K,
V>
-
descendingKeySet
- Specified by:
descendingKeySet
in interfaceNavigableMap<K,
V> - Specified by:
descendingKeySet
in interfacePSortedMap<K,
V>
-
descendingMap
- Specified by:
descendingMap
in interfaceNavigableMap<K,
V> - Specified by:
descendingMap
in interfacePSortedMap<K,
V>
-
entrySet
-
firstEntry
- Specified by:
firstEntry
in interfaceNavigableMap<K,
V>
-
firstKey
-
floorEntry
- Specified by:
floorEntry
in interfaceNavigableMap<K,
V>
-
floorKey
- Specified by:
floorKey
in interfaceNavigableMap<K,
V>
-
get
-
headMap
-
headMap
- Specified by:
headMap
in interfaceNavigableMap<K,
V> - Specified by:
headMap
in interfacePSortedMap<K,
V>
-
higherEntry
- Specified by:
higherEntry
in interfaceNavigableMap<K,
V>
-
higherKey
- Specified by:
higherKey
in interfaceNavigableMap<K,
V>
-
isEmpty
public boolean isEmpty() -
keySet
-
lastEntry
- Specified by:
lastEntry
in interfaceNavigableMap<K,
V>
-
lastKey
-
lowerEntry
- Specified by:
lowerEntry
in interfaceNavigableMap<K,
V>
-
lowerKey
- Specified by:
lowerKey
in interfaceNavigableMap<K,
V>
-
minus
-
minusAll
-
minusFirstEntry
- Specified by:
minusFirstEntry
in interfacePSortedMap<K,
V> - Returns:
- This map, minus its first mapping (the mapping with the least/lowest key).
-
minusLastEntry
- Specified by:
minusLastEntry
in 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:
subMap
in interfaceNavigableMap<K,
V> - Specified by:
subMap
in interfacePSortedMap<K,
V>
-
tailMap
-
tailMap
- Specified by:
tailMap
in interfaceNavigableMap<K,
V> - Specified by:
tailMap
in interfacePSortedMap<K,
V>
-