import ballerina/io;public function main() {
    map<string> m;
    map<string> 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 line2 = addrMap.get("line2");
    io:println(line2);
    boolean hasPostalCode = addrMap.hasKey("postalCode");
    io:println(hasPostalCode);
    addrMap["postalCode"] = "00300";
    io:println(addrMap);
    io:println(addrMap.keys());
    io:println(addrMap.length());
    string removedElement = addrMap.remove("postalCode");
    io:println(addrMap);
    addrMap.forEach(function (string value) {
        io:println(value);
    });    map<int> marks = { sam: 50, jon: 60 };
    map<int> modifiedMarks = marks.entries().map(function ([string, int] pair) returns int {
        var [name, score] = pair;
        io:println(io:sprintf("%s scored: %d", name, score));
        return score + 10;
    });
    io:println(modifiedMarks);
    
    map<string> stringMap = {};
    stringMap["index"] = "100892N";
    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<string> m;

Declares a map constrained by the type string.

    map<string> 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);

Retrieves a value using member access.

    string line2 = addrMap.get("line2");
    io:println(line2);

Retrieves a value using the .get() method. Panics if the map does not have a member with the specified key.

    boolean hasPostalCode = addrMap.hasKey("postalCode");
    io:println(hasPostalCode);

The .hasKey() function checks if a map contains a specified key.

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

Adds or updates 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());

Prints the number of mappings in the map.

    string removedElement = addrMap.remove("postalCode");
    io:println(addrMap);

Mappings can be removed using the .remove() method.

    addrMap.forEach(function (string value) {
        io:println(value);
    });

Maps support functional iteration.

    map<int> marks = { sam: 50, jon: 60 };
    map<int> modifiedMarks = marks.entries().map(function ([string, int] pair) returns int {
        var [name, score] = pair;
        io:println(io:sprintf("%s scored: %d", name, score));
        return score + 10;
    });
    io:println(modifiedMarks);
    

Calling the .entries() method on a map will return the key (string) and the value as a tuple variable.

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

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

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

The return type of member access will be T? where T is the constraint type of the map. If the key does not exist, nil (()) is returned. The Elvis operator ?: is a conditional operator that handles (). 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
Palm Grove
false
line1=No. 20 line2=Palm Grove city=Colombo 03 country=Sri Lanka postalCode=00300
line1 line2 city country postalCode
5
line1=No. 20 line2=Palm Grove city=Colombo 03 country=Sri Lanka
No. 20
Palm Grove
Colombo 03
Sri Lanka
sam scored: 50
jon scored: 60
sam=60 jon=70
100892N