type
constructors
make |
Constructor for type (must be Map type). |
---|
fields
caseInsensitive |
This field configures case sensitivity for maps with Str keys. |
---|---|
def |
The default value to use for |
ordered |
When set to true, the map maintains the order in which key/value pairs are added to the map. |
methods
add |
Add the specified key/value pair to the map. |
---|---|
addAll |
Append the specified map to this map by adding every key/value in m in this map. |
addIfNotNull |
Deprecated - use addNotNull |
addList |
Add the specified list to this map where the values are the list items and the keys are derived by calling the specified function on each item. |
addNotNull |
Call |
all |
Return true if c returns true for all of the key/value pairs in the map. |
any |
Return true if c returns true for any of the key/value pairs in the map. |
clear |
Remove all key/value pairs from the map. |
containsKey |
Return if the specified key is mapped. |
dup |
Create a shallow duplicate copy of this map. |
each |
Call the specified function for every key/value pair in the map. |
eachWhile |
Iterate every key/value pair in the map until the function returns non-null. |
equals |
Two Maps are equal if they have the same type and same number of equal key/value pairs. |
exclude |
Return a new map containing the key/value pairs for which c returns false. |
find |
Return the first value in the map for which c returns true. |
findAll |
Return a new map containing the key/value pairs for which c returns true. |
findNotNull |
Return a new map containing all the key/value pairs where value is not null. |
get |
Get the value for the specified key. |
getChecked |
Get the value for the specified key. |
getOrAdd |
Get the value for the specified key, or if it doesn't exist then automatically add it. |
getOrThrow |
Get the value for the specified key or if key is not mapped then raise UnknownKeyErr. |
hash |
Return platform dependent hashcode based on hash of the keys and values. |
isEmpty |
Return if size() == 0. |
isRO |
Return if this Map is readonly. |
isRW |
Return if this Map is read-write. |
join |
Return a string by concatenating each key/value pair using the specified separator string. |
keys |
Get a list of all the mapped keys. |
map |
Create a new map with the same keys, but apply the specified closure to generate new values. |
mapNotNull |
Convenience for |
reduce |
Reduce is used to iterate through every key/value pair in the map to reduce the map into a single value called the reduction. |
remove |
Remove the key/value pair identified by the specified key from the map and return the value. |
ro |
Get a readonly Map instance with the same contents as this Map (although its values may be mutable themselves). |
rw |
Get a read-write, mutable Map instance with the same contents as this Map. |
set |
Set the value for the specified key. |
setAll |
Append the specified map to this map by setting every key/value in m in this map. |
setList |
Add the specified list to this map where the values are the list items and the keys are derived by calling the specified function on each item. |
setNotNull |
Call |
size |
Get the number of key/value pairs in the list. |
toCode |
Get this map as a Fantom expression suitable for code generation. |
toStr |
Return a string representation of the Map. |
vals |
Get a list of all the mapped values. |
Slot Details
add
src
M add(K key, V val)
Add the specified key/value pair to the map. If the key is already mapped, then throw ArgErr. Return this. If key does not return true for Obj.isImmutable, then throw NotImmutableErr. If key is null throw NullErr. Throw ReadonlyErr if readonly.
addAll
src
M addAll(M m)
Append the specified map to this map by adding every key/value in m in this map. If any key in m is already mapped then this method will fail (any previous keys will remain mapped potentially leaving this map in an inconsistent state). Return this. Throw ReadonlyErr if readonly. Also see setAll
. This method is semanatically equivalent to:
m.each |v, k| { this.add(k, v) }
addIfNotNull
src
@Deprecated { msg="Use addNotNull" }
M addIfNotNull(K key, V? val)
Deprecated - use addNotNull
addList
src
M addList(V[] list, |V,Int->K|? c := null)
Add the specified list to this map where the values are the list items and the keys are derived by calling the specified function on each item. If the function is null, then the items themselves are used as the keys. If any key is already mapped then this method will fail (any previous keys will remain mapped, potentially leaving this map in an inconsistent state). Return this. Throw ReadonlyErr if readonly. Also see setList
.
Examples:
m := [0:"0"] m.addList(["1","2"]) |Str s->Int| { return s.toInt } m => [0:"0", 1:"1", 2:"2"]
addNotNull
all
any
caseInsensitive
src
Bool caseInsensitive := false
This field configures case sensitivity for maps with Str keys. When set to true, Str keys are compared without regard to case for the following methods: get, containsKey, set, add, setAll, addAll, and remove methods. Only ASCII character case is taken into account. The original case is preserved (keys aren't made all lower or upper case). This field defaults to false.
Getting this field is readonly safe. If you attempt to set this method on a map which is not empty or not typed to use Str keys, then throw UnsupportedOperation. Throw ReadonlyErr if set when readonly. This mode cannot be used concurrently with ordered
.
clear
src
M clear()
Remove all key/value pairs from the map. Return this. Throw ReadonlyErr if readonly.
containsKey
def
dup
src
M dup()
Create a shallow duplicate copy of this map. The keys and values themselves are not duplicated. The resulting map is always read/write. This method is readonly safe.
each
eachWhile
equals
exclude
Return a new map containing the key/value pairs for which c returns false. If c returns true for every item, then return an empty map. The inverse of this method is findAll
. If this map is ordered
or caseInsensitive
, then the resulting map is too. This method is readonly safe.
Example:
map := ["off":0, "slow":50, "fast":100] map.exclude |Int v->Bool| { return v == 0 } => ["slow":50, "fast":100]
find
findAll
Return a new map containing the key/value pairs for which c returns true. If c returns false for every item, then return an empty map. The inverse of this method is exclude
. If this map is ordered
or caseInsensitive
, then the resulting map is too. This method is readonly safe.
findNotNull
src
M findNotNull()
Return a new map containing all the key/value pairs where value is not null. If this map is ordered
or caseInsensitive
, then the resulting map is too. This method is readonly safe.
get
getChecked
getOrAdd
src
V getOrAdd(K key, |K->V| valFunc)
Get the value for the specified key, or if it doesn't exist then automatically add it. The value function is called to get the value to add, it is only called if the key is not mapped. Throw ReadonlyErr if readonly only if add is required.
getOrThrow
src
V getOrThrow(K key)
Get the value for the specified key or if key is not mapped then raise UnknownKeyErr. This method is readonly safe.
hash
isEmpty
isRO
Return if this Map is readonly. A readonly Map is guaranteed to be immutable (although its values may be mutable themselves). Any attempt to modify a readonly Map will result in ReadonlyErr. Use rw
to get a read-write Map from a readonly Map. Methods documented as "readonly safe" may be used safely with a readonly Map. This method is readonly safe.
isRW
join
src
Str join(Str separator, |V,K->Str|? c := null)
Return a string by concatenating each key/value pair using the specified separator string. If c is non-null then it is used to format each pair into a string, otherwise "$k: $v" is used. This method is readonly safe.
Example:
m := ["a" : 1, "b" : 2] m.join(" and ") |Int v, Str k->Str| { return "$v from $k" } => 1 from a and 2 from b
keys
src
K[] keys()
Get a list of all the mapped keys. This method is readonly safe.
make
map
src
Obj:Obj? map(|V,K->Obj?| c)
Create a new map with the same keys, but apply the specified closure to generate new values. The new map is typed based on the return type of c. If this map is ordered
or caseInsensitive
, then the resulting map is too. This method is readonly safe.
Example:
m := [2:2, 3:3, 4:4] x := m.map |Int v->Int| { return v*2 } x => [2:4, 3:6, 4:8]
mapNotNull
ordered
When set to true, the map maintains the order in which key/value pairs are added to the map. The implementation is based on using a linked list in addition to the normal hashmap. This field defaults to false.
Getting this field is readonly safe. If you attempt to set this method on a map which is not empty, then throw UnsupportedOperation. Throw ReadonlyErr if set when readonly. This mode cannot be used concurrently with caseInsensitive
.
reduce
src
Obj? reduce(Obj? init, |Obj?,V,K->Obj?| c)
Reduce is used to iterate through every key/value pair in the map to reduce the map into a single value called the reduction. The initial value of the reduction is passed in as the init parameter, then passed back to the closure along with each key/value pair. This method is readonly safe.
Example:
m := ["2":2, "3":3, "4":4] m.reduce(100) |Obj r, Int v->Obj| { return (Int)r + v } => 109
remove
src
V? remove(K key)
Remove the key/value pair identified by the specified key from the map and return the value. If the key was not mapped then return null. Throw ReadonlyErr if readonly.
ro
src
M ro()
Get a readonly Map instance with the same contents as this Map (although its values may be mutable themselves). If this Map is already readonly, then return this. Only methods documented as "readonly safe" may be used safely with a readonly Map, all others will throw ReadonlyErr. This method is readonly safe.
rw
src
M rw()
Get a read-write, mutable Map instance with the same contents as this Map. If this Map is already read-write, then return this. This method is readonly safe.
set
src
@Operator
M set(K key, V val)
Set the value for the specified key. If the key is already mapped, this overwrites the old value. If key is not yet mapped this adds the key/value pair to the map. Return this. If key does not return true for Obj.isImmutable, then throw NotImmutableErr. If key is null throw NullErr. Throw ReadonlyErr if readonly.
setAll
src
M setAll(M m)
Append the specified map to this map by setting every key/value in m in this map. Keys in m not yet mapped are added and keys already mapped are overwritten. Return this. Throw ReadonlyErr if readonly. Also see addAll
. This method is semanatically equivalent to:
m.each |v, k| { this.set(k, v) }
setList
src
M setList(V[] list, |V,Int->K|? c := null)
Add the specified list to this map where the values are the list items and the keys are derived by calling the specified function on each item. If the function is null, then the items themselves are used as the keys. If any key is already mapped then it is overwritten. Return this. Throw ReadonlyErr if readonly. Also see addList
.
Examples:
m := [0:"0", 2:"old"] m.setList(["1","2"]) |Str s->Int| { return s.toInt } m => [0:"0", 1:"1", 2:"2"]
setNotNull
size
toCode
toStr
vals
src
V[] vals()
Get a list of all the mapped values. This method is readonly safe.