Package fj.data
Class TreeMap<K,V>
java.lang.Object
fj.data.TreeMap<K,V>
An immutable, in-memory map, backed by a red-black tree.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> TreeMap <K, V> arrayTreeMap
(Ord<K> keyOrd, P2<K, V>... ps) Constructs a tree map from the given elements.boolean
Determines if the given key value exists in this tree map.Deletes the entry in the tree map that corresponds to the given key.static <K,
V> TreeMap <K, V> Constructs an empty tree map.boolean
static <K,
V> TreeMap <K, V> fromMutableMap
(Ord<K> ord, Map<K, V> m) An immutable projection of the given mutable map.get()
Returns a first-class version of the get method for this TreeMap.Returns a potential value that the given key maps to.int
hashCode()
boolean
isEmpty()
Determines if this tree map has any entries.static <K,
V> TreeMap <K, V> iterableTreeMap
(Ord<K> keyOrd, Iterable<P2<K, V>> it) Constructs a tree map from the given elements.iterator()
Returns an iterator for this map's key-value pairs.static <K,
V> TreeMap <K, V> iteratorTreeMap
(Ord<K> keyOrd, Iterator<P2<K, V>> it) Constructs a tree map from the given elements.keys()
Returns all keys in this tree map.Maps the given function across the values of this TreeMap.max()
Returns the maximum (key, value) pair in the tree if the tree is not empty.maxKey()
Returns the maximum key in the tree if the tree is not empty.min()
Returns the minimum (key, value) pair in the tree if the tree is not empty.minKey()
Returns the minimum key in the tree if the tree is not empty.Inserts the given key and value association into the tree map.static <K,
V> TreeMap <K, V> setTreeMap
(Ord<K> ord, Set<P2<K, Option<V>>> s) Constructs a TreeMap from the given set.int
size()
Returns the number of entries in this tree map.Splits this TreeMap at the given key.splitLookup
(K k) Splits this TreeMap at the given key.toList()
A mutable map projection of this tree map.toStream()
toString()
static <K,
V> TreeMap <K, V> Constructs a tree map from the given elements.The expressiont1.union(t2)
takes the left-biased union oft1
andt2
.The expressiont1.union(t2)
takes the left-biased union oft1
andt2
.Modifies the value for the given key, if present, by applying the given function to it.Modifies the value for the given key, if present, by applying the given function to it, or inserts the given value if the key is not present.values()
Returns all values in this tree map.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
tree
-
-
Constructor Details
-
TreeMap
-
-
Method Details
-
ord
-
empty
Constructs an empty tree map.- Parameters:
keyOrd
- An order for the keys of the tree map.- Returns:
- an empty TreeMap with the given key order.
-
equals
-
hashCode
public int hashCode() -
toString
-
treeMap
Constructs a tree map from the given elements.- Parameters:
keyOrd
- An order for the keys of the tree map.p2s
- The elements to construct the tree map with.- Returns:
- a TreeMap with the given elements.
-
iterableTreeMap
Constructs a tree map from the given elements.- Parameters:
keyOrd
- An order for the keys of the tree map.it
- The elements to construct the tree map with.- Returns:
- A TreeMap with the given elements.
-
iteratorTreeMap
Constructs a tree map from the given elements.- Parameters:
keyOrd
- An order for the keys of the tree map.it
- The elements to construct the tree map with.- Returns:
- A TreeMap with the given elements.
-
arrayTreeMap
Constructs a tree map from the given elements.- Parameters:
keyOrd
- An order for the keys of the tree map.ps
- The elements to construct the tree map with.- Returns:
- A TreeMap with the given elements.
-
get
Returns a potential value that the given key maps to.- Parameters:
k
- The key to look up in the tree map.- Returns:
- A potential value for the given key.
-
set
Inserts the given key and value association into the tree map. If the given key is already mapped to a value, the old value is replaced with the given one.- Parameters:
k
- The key to insert.v
- The value to insert.- Returns:
- A new tree map with the given value mapped to the given key.
-
delete
Deletes the entry in the tree map that corresponds to the given key.- Parameters:
k
- The key to delete from this tree map.- Returns:
- A new tree map with the entry corresponding to the given key removed.
-
size
public int size()Returns the number of entries in this tree map.- Returns:
- The number of entries in this tree map.
-
isEmpty
public boolean isEmpty()Determines if this tree map has any entries.- Returns:
true
if this tree map has no entries,false
otherwise.
-
values
Returns all values in this tree map.- Returns:
- All values in this tree map.
-
keys
Returns all keys in this tree map.- Returns:
- All keys in this tree map.
-
contains
Determines if the given key value exists in this tree map.- Parameters:
k
- The key value to look for in this tree map.- Returns:
true
if this tree map contains the given key,false
otherwise.
-
iterator
Returns an iterator for this map's key-value pairs. This method exists to permit the use in afor
-each loop. -
toMutableMap
A mutable map projection of this tree map.- Returns:
- A new mutable map isomorphic to this tree map.
-
toStream
-
toStreamReverse
-
toList
-
toListReverse
-
fromMutableMap
An immutable projection of the given mutable map.- Parameters:
ord
- An order for the map's keys.m
- A mutable map to project to an immutable one.- Returns:
- A new immutable tree map isomorphic to the given mutable map.
-
get
Returns a first-class version of the get method for this TreeMap.- Returns:
- a functional representation of this TreeMap.
-
update
Modifies the value for the given key, if present, by applying the given function to it.- Parameters:
k
- The key for the value to modify.f
- A function with which to modify the value.- Returns:
- A new tree map with the value for the given key transformed by the given function, paired with True if the map was modified, otherwise False.
-
update
Modifies the value for the given key, if present, by applying the given function to it, or inserts the given value if the key is not present.- Parameters:
k
- The key for the value to modify.f
- A function with which to modify the value.v
- A value to associate with the given key if the key is not already present.- Returns:
- A new tree map with the value for the given key transformed by the given function.
-
split
Splits this TreeMap at the given key. Returns a triple of:- A set containing all the values of this map associated with keys less than the given key.
- An option of a value mapped to the given key, if it exists in this map, otherwise None.
- A set containing all the values of this map associated with keys greater than the given key.
- Parameters:
k
- A key at which to split this map.- Returns:
- Two sets and an optional value, where all elements in the first set are mapped to keys less than the given key in this map, all the elements in the second set are mapped to keys greater than the given key, and the optional value is the value associated with the given key if present, otherwise None.
-
setTreeMap
Constructs a TreeMap from the given set.- Parameters:
ord
- An order for the keys of the tree map.s
- The elements to construct the tree map with.- Returns:
- a TreeMap with the given elements.
-
splitLookup
Splits this TreeMap at the given key. Returns a triple of:- A tree map containing all the values of this map associated with keys less than the given key.
- An option of a value mapped to the given key, if it exists in this map, otherwise None.
- A tree map containing all the values of this map associated with keys greater than the given key.
- Parameters:
k
- A key at which to split this map.- Returns:
- Two tree maps and an optional value, where all keys in the first tree map are mapped to keys less than the given key in this map, all the keys in the second tree map are mapped to keys greater than the given key, and the optional value is the value associated with the given key if present, otherwise None.
-
map
Maps the given function across the values of this TreeMap.- Parameters:
f
- A function to apply to the values of this TreeMap.- Returns:
- A new TreeMap with the values transformed by the given function.
-
min
Returns the minimum (key, value) pair in the tree if the tree is not empty. -
minKey
Returns the minimum key in the tree if the tree is not empty. -
max
Returns the maximum (key, value) pair in the tree if the tree is not empty. -
maxKey
Returns the maximum key in the tree if the tree is not empty. -
union
The expressiont1.union(t2)
takes the left-biased union oft1
andt2
. It preferst1
when duplicate keys are encountered.- Parameters:
t2
- The other tree we wish to combine with this one- Returns:
- The combined TreeMap
-
union
The expressiont1.union(t2)
takes the left-biased union oft1
andt2
. It preferst1
when duplicate keys are encountered.- Parameters:
t2
- The other list/set of pairs we wish to combine with this one- Returns:
- The combined TreeMap
-