This API documentation is for Ballerina 1.2.0. View API documentation for the latest release.

Functions - lang.map

entries

Returns a map containing [key, member] pair as the value for each key.

filter

Selects the members from a map for which a function returns true.

forEach

Applies a function to each member of a map.

get

Returns the member of map m with key k.

hasKey

Tests whether m has a member with key k.

iterator

Returns an iterator over a map.

keys

Returns a list of all the keys of map m.

length

Returns number of members of a map.

map

Applies a function each member of a map and returns a map of the result.

reduce

Combines the members of a map using a combining function.

remove

Removes a member of a map.

removeAll

Removes all members of a map.

removeIfHasKey

Removes a member of a map with a given key, if the map has member with the key.

toArray

Returns a list of all the members of a map.

entries

(map<Type> m)

returns map<[string, Type]>

Returns a map containing [key, member] pair as the value for each key.

Parameters

  • m map<Type>
  • the map

  • Return Type

    (map<[string, Type]>)
  • a new map of [key, member] pairs

filter

(map<Type> m, function(Type) returns (boolean) func)

returns map<Type>

Selects the members from a map for which a function returns true.

Parameters

  • m map<Type>
  • the map

  • func function(Type) returns (boolean)
  • a predicate to apply to each element to test whether it should be included

  • Return Type

    (map<Type>)
  • new map containing members for which func evaluates to true

forEach

(map<Type> m, function(Type) returns (()) func)

Applies a function to each member of a map. The parameter func is applied to each member of m.

Parameters

  • m map<Type>
  • the map

  • func function(Type) returns (())
  • a function to apply to each member

get

(map<Type> m, string k)

returns Type

Returns the member of map m with key k. This for use in a case where it is known that the map has a specific key, and accordingly panics if m does not have a member with key k.

Parameters

  • m map<Type>
  • the map

  • k string
  • the key

  • Return Type

    (Type)
  • member with key k

hasKey

(map<Type> m, string k)

returns boolean

Tests whether m has a member with key k.

Parameters

  • m map<Type>
  • the map

  • k string
  • the key

  • Return Type

    (boolean)
  • true if m has a member with key k

iterator

(map<Type> m)

returns object { public function next () returns (record {| (any|error) value; |}?); }

Returns an iterator over a map. The iterator will iterate over the members of the map not the keys. The entries function can be used to iterate over the keys and members together. The keys function can be used to iterator over just the keys.

Parameters

  • m map<Type>
  • the map

  • Return Type

    (object { public function next () returns (record {| (any|error) value; |}?); })
  • a new iterator object that will iterate over the members of m

keys

(map<any | error> m)

returns string[]

Returns a list of all the keys of map m.

Parameters

  • m map<any | error>
  • the map

  • Return Type

    (string[])
  • a new list of all keys

length

(map<any | error> m)

returns int

Returns number of members of a map.

Parameters

  • m map<any | error>
  • the map

  • Return Type

    (int)
  • number of members in m

map

(map<Type> m, function(Type) returns (Type1) func)

returns map<Type1>

Applies a function each member of a map and returns a map of the result. The resulting map will have the same keys as the argument map.

Parameters

  • m map<Type>
  • the map

  • func function(Type) returns (Type1)
  • a function to apply to each member

  • Return Type

    (map<Type1>)
  • new map containing result of applying parameter func to each member

reduce

(map<Type> m, function(Type1, Type) returns (Type1) func, Type1 initial)

returns Type1

Combines the members of a map using a combining function. The combining function takes the combined value so far and a member of the map, and returns a new combined value.

Parameters

  • m map<Type>
  • the map

  • func function(Type1, Type) returns (Type1)
  • combining function

  • initial Type1
  • initial value for the first argument of combining parameter func

  • Return Type

    (Type1)
  • result of combining the members of m using func

remove

(map<Type> m, string k)

returns Type

Removes a member of a map.

Parameters

  • m map<Type>
  • the map

  • k string
  • the key

  • Return Type

    (Type)
  • the member of m that had key k This removed the member of m with key k and returns it. It panics if there is no such member.

removeAll

(map<any | error> m)

Removes all members of a map. This panics if any member cannot be removed.

Parameters

  • m map<any | error>
  • the map

removeIfHasKey

(map<Type> m, string k)

returns Type?

Removes a member of a map with a given key, if the map has member with the key.

Parameters

  • m map<Type>
  • the map

  • k string
  • the key

  • Return Type

    (Type?)
  • the member of m that had key k, or () if m does not have a key k If m has a member with key k, it removes and returns it; otherwise it returns ().

toArray

(map<Type> m)

returns Type[]

Returns a list of all the members of a map.

Parameters

  • m map<Type>
  • the map

  • Return Type

    (Type[])
  • an array whose members are the members of m