import ballerina/io;

public function main() {
    // Creates a `map` constrained by the `int` type.
    map<int> m = {
        "x": 1,
        "y": 2
    };

    // Gets the entry for `x`.
    int? v = m["x"];

    io:println(v);

    // Adds a new entry for `z`.
    m["z"] = 5;

    // Using `m["x"]` wouldn't work here because type would be `int?`,
    // not `int`.
    m["z"] = m.get("x");

}

Maps

The map<T> type is a mapping from strings to T. The map syntax is similar to JSON. Maps are mutable. m[k] is an “lvalue”. foreach will iterate over the values of the map. m[k] gets entry for k; nil if missing. Use m.get(k) when you know that there is an entry for k. m.keys() can be used to iterate over keys to get the keys as an array of strings. == and != on maps is deep. Two maps are equal if they have the same set of keys and the values for each key are equal.

import ballerina/io;
public function main() {
    map<int> m = {
        "x": 1,
        "y": 2
    };

Creates a map constrained by the int type.

    int? v = m["x"];

Gets the entry for x.

    io:println(v);
    m["z"] = 5;

Adds a new entry for z.

    m["z"] = m.get("x");

Using m["x"] wouldn’t work here because type would be int?, not int.

}
bal run maps.bal
1