Swan Lake Beta3 is here.
import ballerina/io;

public function main() {
    // Creates a `map` constrained by the type `int`.
    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 map from strings to T. 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 type int.

    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
import ballerina/io;

public function main() {
    // Creates a `map` constrained by the type `int`.
    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 map from strings to T. 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 type int.

    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
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand