89DEVs

Mapping in Solidity

The mapping in Solidity is like a hash table and very similar to a dictionary in Python. It is used to store data in key-value pairs. The key can be any value data type in Solidity like address, bool or integers and the value can be of any type. Because the mapping doesn't store any values, just references, it is considered a reference data type in Solidity. A common way to use a mapping is to map an address to a related value.

You can think of mappings as hash tables, which are virtually initialised such that every possible key exists and is mapped to a value whose byte-representation is all zeros, a type’s default value. The similarity ends there, the key data is not stored in a mapping, only its keccak256 hash is used to look up the value.

syntax of Mapping

The syntax of the mapping data type is demonstrated below. The mapping is declared using the mapping keyword followed by the key-value pair, the access modifier and the name of the mapping. It is also possible to create nested mappings by having another mapping as value of the parent mapping. // syntax of a mapping in Solidity mapping(key => value) [access_modifier] [name]; // syntax of a nested mapping in Solidity mapping(key => mapping(key => value)) [access_modifier] [name];

example of Mapping

To understand the concept of Mapping in Solidity better, let's create an example mapping. We can associate an address with a score or rating, this could be useful for games or to rate addresses based on some algorithm. The addresses have an address data type and the score will have an unsigned integer (uint) data type. We will learn how to declare a mapping, set values, get values and delete values of the mapping.

declare a Mapping

To declare a new mapping we use the mapping keyword and then specify the data types of the key and value pair followed by the name of the mapping. // example mapping in Solidity mapping (address => uint) scores;

add values to Mapping

To add a new key-value pair to the mapping we declare a setter function, which accepts an address and a score. The key-value pair is then added to the mapping. In the next step we write a getter function that can receive the value for a specific key, in our example an ethereum address. // set new key-value pair in Mapping function set_val(address _addr, uint score) public { scores[_addr] = score; } To update an value we can just set a new value for the same address and the old value will be overwritten.

get values from Mapping

To return the value (score) for a specific key (here an ethereum address) from the Mapping we can write a getter function like shown below. All we need to do to use it is to pass the address of which we want to know the score. // get value for key from Mapping function get_val(address _addr) public view returns(uint) { return scores[_addr]; }

delete value of Mapping

To delete a key-value pair we define a function that accepts an address and then deletes the key-value pair from the mapping. The item in the mapping is delete by using the delete keyword. // delete key-value pair from Mapping function del_val(address _addr) public { delete scores[_addr]; }

size of Mapping

Mappings in Solidity are not iterable and don't have a length. Therefore there is no way to return the size of a Mapping.

        

Summary

Click to jump to section