MAP

Map blocks represent a mapping between a key and a value. A map contains unique keys. Only keys of type String are available.

Map creation

To create a Map, connect the data type to the map block. The type you will pass as a parameter will be the type Code Blocks will store the values with. Here is the Java code and the corresponding Code Blocks interpretation:

HashMap<String, Long> string_keys_int_values = new HashMap<String, Long>();

HashMap<String, MultilingualString> string_keys_string_values = new HashMap<String, MultilingualString>();

Adding elements

In order to add the key-value pair to the Map, you can use the map-set block. The insertion order is not retained in the Map. Internally, a separate hash is generated for every element, and the elements are indexed based on this hash to make it more efficient.

Changing elements

After adding the elements, if you wish to change the element, you can do it by using the map-set block. The value of the key can be changed by simply inserting the updated value for the key.

Removing elements

In order to remove an element from the Map, you can use the map-remove block. This block takes the map object and the key value and removes the mapping for a key from this map if it is present.

Getting Elements

To get an element from the Map, you can use the map-get block. This block takes the map object and the key value and returns a type of the value. The return type is defined on the map creation.

Result:

Characteristics of a Map

  1. A Map cannot contain duplicate elements.
  2. Each key can map at most one value. The key must be of the String type.
  3. Iteration of elements is not provided in the Map.
  4. Most frequently is used with the REST blocks to parse a JSON response.
  5. You can use a Map object as a parameter in the function, containing multiple key-values.