import ballerina/io;public function main() {
    map<any> m;
    map<any> addrMap = { line1: "No. 20", line2: "Palm Grove",
        city: "Colombo 03", country: "Sri Lanka" };
    io:println(addrMap);
    string country = <string>addrMap["country"];
    io:println(country);
    string city = <string>addrMap.city;
    io:println(city);
    addrMap["postalCode"] = "00300";
    addrMap.postalCode = "00301";
    io:println(addrMap);
    io:println(addrMap.keys());
    io:println(addrMap.length());
    boolean isRemoved = addrMap.remove("postalCode");
    io:println(addrMap);
    map<string> stringMap = {};
    stringMap["index"] = "100892N";
    string index = stringMap.index;
    io:println(index);
    string index2 = stringMap["index"] ?: "";
    io:println(index2);
}

Maps

The map type in Ballerina defines a mutable mapping from keys that are strings to values of the type specified as the map’s constraint type.

import ballerina/io;
public function main() {
    map<any> m;

Declare a map constrained by the type any.

    map<any> addrMap = { line1: "No. 20", line2: "Palm Grove",
        city: "Colombo 03", country: "Sri Lanka" };
    io:println(addrMap);

You can also declare and initialize a map with a map literal.

    string country = <string>addrMap["country"];
    io:println(country);

Retrieve a value using index-based access.

    string city = <string>addrMap.city;
    io:println(city);

Retrieve a value using field-based access.

    addrMap["postalCode"] = "00300";
    addrMap.postalCode = "00301";
    io:println(addrMap);

Add or update the value of a key.

    io:println(addrMap.keys());

The keys() method returns the keys of the map as an array.

    io:println(addrMap.length());

Print the number of mappings in the map.

    boolean isRemoved = addrMap.remove("postalCode");
    io:println(addrMap);

Mappings can be removed using the remove() method.

    map<string> stringMap = {};
    stringMap["index"] = "100892N";

Maps can only contain values of the type specified by the constraint type descriptor.

    string index = stringMap.index;
    io:println(index);

You do not need explicit conversion to string when retrieving a value from stringMap via field-based access.

    string index2 = stringMap["index"] ?: "";
    io:println(index2);
}

Note that the return type of index-based access will be T? (where T is the constraint type of the map). If the key does not exist,nilis returned. Elvis operator?:is a conditional operator that handlesnil`. If the given expression evaluates to nil, the second expression is evaluated and its value is returned.

# To run this sample, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command.
$ ballerina run maps.bal
{"line1":"No. 20", "line2":"Palm Grove", "city":"Colombo 03", "country":"Sri Lanka"}
Sri Lanka
Colombo 03
{"line1":"No. 20", "line2":"Palm Grove", "city":"Colombo 03", "country":"Sri Lanka",
    "postalCode":"00301"}
["line1", "line2", "city", "country", "postalCode"]
5
{"line1":"No. 20", "line2":"Palm Grove", "city":"Colombo 03", "country":"Sri Lanka"}
100892N
100892N