Skip to content

Map

Type

Map<K, V> type is used to represent values that hold key-to-value mappings, where the key is of type Kand the value of type V. Maps are instantiated using mapOf().

Methods

filter

<function> Returns a map with only those entries that match the predicate. In other words, returns a Map containing only those entries for which the predicate returns true.

Receiver

Map<K, V>

Type Arguments

K - map key type

V - map value type

Arguments

predicate - (K, V) -> Boolean - a predicate that determines true or false based on the entry's key and value

Returns

Map<K, V> - a map containing entries matching the predicate

Usage
mapOf<Text, Number>(Pair("a", 1), Pair("b", 2), Pair("c", 10))
    .filter(function(k: Text, v: Number) -> v < 10 && k != "b")
== mapOf<Text, Number>(Pair("a", 1))

getOrNone

<function> Returns an optional Some<V> containing the value associated with key if it exsits, or an empty None.

Receiver

Map<K, V>

Type Arguments

K - map key type

V - map value type

Arguments

key - K - to retrieve value for

Returns

Optional<V>

Usage

mapOf<Text, Number>().getOrNone("a") == optionalOf<Number>()
mapOf<Text, Number>(Pair("a", 1), Pair("b", 2)).getOrNone("b")
== optionalOf<Number>(2)

keys

<function> Returns a set with all the keys in the Map.

Receiver

Map<K, V>

Type Arguments

K - map key type

V - map value type

Returns

Set<K>

Usage

mapOf<Text, Number>().keys() == setOf<Text>()
mapOf<Text, Number>(Pair("a", 1), Pair("b", 2)).keys() == setOf("a", "b")

mapValues

<function> Returns a Map<K, V> where the values of type V of the entries of the original map are converted to new values of type W in the new map, retaining the same keys.

Receiver

Map<K, V>

Type Arguments

K - map key type

V - the old map value type

W - the new map value type

Arguments

transformer - (K, V) -> W - function which takes a key-value pair of type (K, V) and produces a value of type W

Returns

Map<K, W>

Usage
mapOf<Text, Number>(Pair("a", 4), Pair("b", 3.6))
    .mapValues(function(k: Text, v: Number) -> v.toText())
== mapOf<Text, Text>(Pair("a", "4"), Pair("b", "3.6"))

mapValues

<function> Returns a Map<K, V> where the values of type V of the entries of the original map are converted to new values of type W in the new map, retaining the same keys.

Receiver

Map<K, V>

Type Arguments

K - map key type

V - the old map value type

W - the new map value type

Arguments

transformer - (K, V) -> W - function which takes a key-value pair of type (K, V) and produces a value of type W

Returns

Map<K, W>

Usage
mapOf(Pair("1", 1), Pair("2", 2)).toList() == listOf(Pair("1", 1), Pair("2", 2))

plus

<function> Returns a map concatenating the other map to self, overwriting values in self with values from other if there are duplicate keys. The infix operator is +. Note that if other has different (but compatible) types for K and/or V, the resulting type will be that of self (or the left-hand side if using the infix operator).

Receiver

Map<K, V>

Type Arguments

K - map key type

V - map value type

Arguments

other - Map<K, V> - the map to concatenate to self

Returns

Map<K, V> - a map containing the concatenation of the two maps

Usage

Observe the value associated with b below. The key occurs in both maps and its final value depends on which map is concatenated to which.

mapOf<Text, Number>(Pair("a", 1), Pair("b", 2))
+ mapOf<Text, Number>(Pair("b", 3), Pair("c", 4))
== mapOf<Text, Number>(Pair("a", 1), Pair("b", 3), Pair("c", 4))
mapOf<Text, Number>(Pair("b", 3), Pair("c", 4))
+ mapOf<Text, Number>(Pair("a", 1), Pair("b", 2))
== mapOf<Text, Number>(Pair("a", 1), Pair("b", 2), Pair("c", 4))

values

<function> Returns a list with all values present in the map. Values are returned in their insertion order. Note that the resulting list may contain duplicate values.

Receiver

Map<K, V>

Type Arguments

K - map key type

V - map value type

Returns

List<V>

Usage

mapOf<Text, Number>().values() == listOf<Number>()
mapOf<Text, Number>(Pair("a", 1), Pair("b", 2)).values() == listOf(1, 2)
mapOf<Text, Number>(Pair("a", 2), Pair("b", 2)).values() == listOf(2, 2)

with

<function> Returns the original map with the new entry added -- or replaced, if already present.

Receiver

Map<K, V>

Type Arguments

K - map key type

V - map value type

Arguments

key - K

value - V

Returns

Map<K, V>

Usage

mapOf<Text, Number>().with("a", 1).with("b", 2).with("c", 3)
== mapOf<Text, Number>(Pair("a", 1), Pair("b", 2), Pair("c", 3))
mapOf<Text, Number>().with("a", 1).with("a", 2)
== mapOf<Text, Number>(Pair("a", 2))

without

<function> Returns the original map without the entry corresponding to the specified key. If no existing entry is found for the specified key, the original map is returned.

Receiver

Map<K, V>

Type Arguments

K - map key type

V - map value type

Arguments

key - K - key for which the corresponding value should be removed

Returns

Map<K, V>

Usage

mapOf<Text, Number>(Pair("a", 1), Pair("b", 2)).without("a")
== mapOf<Text, Number>(Pair("b", 2))
mapOf<Text, Number>(Pair("a", 1), Pair("b", 2)).without("c")
== mapOf<Text, Number>(Pair("a", 1), Pair("b", 2))

Inherited methods

allMatch

<function> Determines whether the predicate is true for all elements in the collection. allMatch on an empty collection returns true.

Receiver

Collection<T>

Type Arguments

T - the element type

Arguments

predicate - (T) -> Boolean - The predicate tested on the elements of the collection

Returns

Boolean - true if predicate returns true for all elements in the collection, false otherwise

Usage

mapOf<Text, Number>().allMatch(function(k: Text, v: Number) -> v < 3) == true
mapOf<Text, Number>(Pair("a", 1), Pair("b", 4))
    .allMatch(function(k: Text, v: Number) -> v < 3)
== false
mapOf<Text, Number>(Pair("a", 1), Pair("c", 2))
    .allMatch(function(k: Text, v: Number) -> v < 3)
== true

anyMatch

<function> Determines whether the predicate is true for at least one element in the collection. anyMatch on an empty collection returns false.

Receiver

Collection<T>

Type Arguments

T - the element type

Arguments

predicate - (T) -> Boolean - The predicate tested on the elements of the collection

Returns

Boolean - true if predicate returns true for any element in the collection, false otherwise

Usage

mapOf<Text, Number>().anyMatch(function(k: Text, v: Number) -> v < 3) == false
mapOf<Text, Number>(Pair("a", 1), Pair("b", 4))
    .anyMatch(function(k: Text, v: Number) -> v < 3)
== true
mapOf<Text, Number>(Pair("c", 3), Pair("b", 4))
    .anyMatch(function(k: Text, v: Number) -> v < 3)
== false

contains

<function> Determines whether the collection contains the given element.

Receiver

Collection<T>

Type Arguments

T - the element type

Arguments

element - T - The element to be searched in the collection.

Returns

Boolean - true if element is in the collection, false otherwise

Usage

mapOf<Text, Number>().contains("a") == false
mapOf<Text, Number>(Pair("a", 1)).contains("a") == true

forEach

<function> Applies a function to each element of a collection in the collection's order. Ignores the return value of the action.

Receiver

Collection<T>

Type Arguments

T - the element type

U - the result type (ignored)

Arguments

action - (T) -> U - the function to call on each element

Usage
// writes 2 debug lines
mapOf<Text, Number>(Pair("a", 4), Pair("b", 3))
    .forEach(function(k: Text, v: Number) -> debug("map entry: (" + k + ", " + v.toText() + ")"));

isEmpty

<function> Determines if the collection contains no elements.

Receiver

Collection<T>

Type Arguments

T - the element type

Returns

Boolean - true if the size of the collection is 0, otherwise returns false.

Usage

mapOf<Number, Number>().isEmpty() == true
mapOf<Number, Number>(Pair(1,2), Pair(2,3)).isEmpty() == false

isNotEmpty

<function> Determines if the collection contains any elements.

Receiver

Collection<T>

Type Arguments

T - the element type

Returns

Boolean - false if the size of the collection is 0, otherwise returns true.

Usage

mapOf<Number, Number>(Pair(1,2), Pair(2,3)).isNotEmpty() == true
mapOf<Number, Number>().isNotEmpty() == false

noneMatch

<function> Determines whether the predicate is false for all elements in the collection. noneMatch on an empty collection returns true.

Receiver

Collection<T>

Type Arguments

T - the element type

Arguments

predicate - (T) -> Boolean - The predicate tested on the elements of the collection

Returns

Boolean - true if predicate returns false for all elements in the collection, false otherwise

Usage

mapOf<Text, Number>().noneMatch(function(k: Text, v: Number) -> v < 3) == true
mapOf<Text, Number>(Pair("a", 5), Pair("b", 4))
    .noneMatch(function(k: Text, v: Number) -> v < 3)
== true
mapOf<Text, Number>(Pair("c", 2), Pair("b", 4))
    .noneMatch(function(k: Text, v: Number) -> v < 3)
== false

size

<function> Gets the size of the collection.

Receiver

Collection<T>

Type Arguments

T - the element type

Returns

Number - the number of elements in the collection

Usage

mapOf<Text, Number>().size() == 0
mapOf<Text, Number>(Pair("a", 1), Pair("b", 2)).size() == 2