Lesson 8 of 34
In Progress

Mappings

In Solidity, mappings are used to store key-value pairs, similar to a dictionary in other languages. Mappings are a powerful and flexible data structure that can be used to store and retrieve data in a contract. In this article, we’ll take a closer look at mappings in Solidity, including how to define and use them, and how to iterate over the keys and values in a mapping.

Defining Mappings

In Solidity, mappings are defined using the “mapping” keyword followed by the key data type, an arrow, and the value data type, and then the variable name. The key data type can be any data type that is hashable (able to be converted to a unique fixed-size value), while the value data type can be any data type.

For example, the following code defines a mapping called “balances” that maps addresses (the keys) to unsigned integers (the values):

mapping(address => uint) balances;

This mapping can be used to store the balance of each address in the contract.

Using Mappings

To access the value of a key in a mapping, use the mapping name followed by the key in square brackets. For example:

balances[msg.sender]++;

This code retrieves the value of the “msg.sender” key in the “balances” mapping and increments it by one.

To set the value of a key in a mapping, use the mapping name followed by the key in square brackets, followed by the assignment operator and the new value. For example:

balances[recipient] = amount;

This code sets the value of the “recipient” key in the “balances” mapping to the “amount” value.

Iterating Over Mapping Keys and Values

In Solidity, it is not possible to directly iterate over the keys and values in a mapping. However, it is possible to create a separate data structure (such as an array) and store the keys or values in it, and then iterate over that data structure.

For example, the following code creates an array called “accounts” and stores all of the keys (addresses) in the “balances” mapping in it:

address[] accounts;

function getAccounts() public {
  for (uint i = 0; i < balances.length; i++) {
    accounts.push(balances[i].key);
  }
}

This code defines a function called “getAccounts” that iterates over the keys in the “balances” mapping and stores them in the “accounts” array.

Conclusion

In conclusion, mappings in Solidity are a powerful and flexible data structure that can be used to store and retrieve key-value pairs. Mappings are defined using the “mapping” keyword and accessed using square brackets and the key. While it is not possible to directly iterate over the keys and values in a mapping, it is possible to create a separate data structure and store the keys or values in it, and then iterate over that data structure. Mappings are a useful tool for storing and managing data in Solidity contracts and decentralized applications.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

What is a mapping in Solidity?

In Solidity, a mapping is a data structure that is used to store key-value pairs. Mappings are similar to a dictionary in other languages and are a useful tool for storing and managing data in contracts and decentralized applications.

How are mappings defined in Solidity?

In Solidity, mappings are defined using the “mapping” keyword followed by the key data type, an arrow, and the value data type, and then the variable name. For example: “mapping(address => uint) balances;”.

How do you access the value of a key in a mapping in Solidity?

To access the value of a key in a mapping in Solidity, use the mapping name followed by the key in square brackets. For example: “balances[msg.sender]++;”.

How do you set the value of a key in a mapping in Solidity?

To set the value of a key in a mapping in Solidity, use the mapping name followed by the key in square brackets, followed by the assignment operator and the new value. For example: “balances[recipient] = amount;”.

Can you iterate over the keys and values in a mapping in Solidity?

In Solidity, it is not possible to directly iterate over the keys and values in a mapping. However, it is possible to create a separate data structure (such as an array) and store the keys or values in it, and then iterate over that data structure.