SortedDict#
SortedDict<K, V> is a java.util.TreeMap<K, V> that keeps its entries sorted by key. It's the type of Worktime.dates, where the keys are Moment values. A new Worktime already holds an empty SortedDict, so you usually fill that map in place with put. To build one yourself, call a constructor.
Declaration#
Every TreeMap, NavigableMap and Map method works as usual. The members below are the ones the class adds.
Constructors#
SortedDict()#
public
Creates an empty map that orders keys by their natural ordering. The keys must implement Comparable, as Moment does.
SortedDict(Map)#
public
Creates a map with the entries of m, ordered by the keys' natural ordering. The map doesn't keep the comparator of m, even when m is a SortedMap.
Parameters
| Name | Type | Description |
|---|---|---|
m |
Map<K, V> |
Entries to copy |
SortedDict(Comparator)#
public
Creates an empty map that orders keys with c.
Parameters
| Name | Type | Description |
|---|---|---|
c |
Comparator<K> |
Key ordering |
Methods#
iterator#
public Iterator
Iterates over entrySet() in key order. Because of this method, a SortedDict works directly in a for-each loop: for (Map.Entry<K, V> e : dict).
Returns an iterator over the entries.
add#
public void
Adds an entry when the key isn't in the map yet.
Parameters
| Name | Type | Description |
|---|---|---|
k |
K |
Key |
v |
V |
Value |
Errors throws IllegalArgumentException (duplicate key) when the map already contains k. The map stays unchanged. Use put to replace an existing entry.
tryGetValue#
public boolean
Looks up k and writes the result to out.value.
Parameters
| Name | Type | Description |
|---|---|---|
k |
K |
Key to look up |
out |
Ref<V> |
Receives the value, or null when the key is missing |
Returns true when the map contains k.
getValueOrDefault#
public V
Same as get(k).
Returns the value for k, or null when the key is missing.
toDictionary#
public <A, B> Dict
Builds a new map from the entries, with keys and values produced by the two functions. Fn1 is a functional interface, so lambdas work for both arguments.
Parameters
| Name | Type | Description |
|---|---|---|
key |
Fn1<Map.Entry<K, V>, A> |
Produces the key of each new entry |
val |
Fn1<Map.Entry<K, V>, B> |
Produces the value of each new entry |
Returns a Dict<A, B>, a LinkedHashMap subclass. It keeps the entries in this map's key order. When two entries produce the same key, the later entry wins. Wrap the result in new SortedDict<>(...) to get a sorted map back.
Example#
;
;
;
Related#
Worktime- the field that holds aSortedDictMoment- the key type of the date exceptionsRef<T>- the out parameter oftryGetValue- Working time and calendars - date exception keys