Erlang - Maps



A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map.

An example of how the Map data type can be used is shown in the following program.

Here we are defining a Map M1 which has 2 mappings. The map_size is an inbuilt function defined in Erlang which can be used to determine the size of the map.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   M1 = #{name=>john,age=>25}, 
   io:fwrite("~w",[map_size(M1)]).

The output of the above program will be as follows.

Output

2

Some of the other methods available for maps are as follows.

Sr.No. Methods & Description
1

from_list

This method is used to generate a map from a list.

2

find

This method is used to find if a particular key exists in the map.

3

get

This method is used to get the value of a particular key in the map.

4

is_key

This method is used to determine if a particular key is defined as a key in the map.

5

keys

This method is used to return all the keys from a map.

6

merge

This method is used to merge 2 maps.

7

put

This method is used to add a key value pair to the map.

8

values

This method is used to return all the values from a map.

9

remove

This method is used to remove a key value from the map.

Advertisements