Mappings in solidity

Mappings in solidity

100 days of solidity (Day 13–17)

What is Solidity mapping?

In Solidity, a mapping is a data structure that associates a key with a value, similar to a dictionary or hash map in other programming languages. You can think of a mapping as a collection of key-value pairs, where each key is unique and corresponds to a specific value.


Simple explanation of mappings in solidity

  1. Declaration: To use a mapping, you need to declare it by specifying the data types for the keys and values. For example, you can declare a mapping that maps addresses to integers as mapping(address => uint) balances;. This mapping associates an address with an unsigned integer value.

  2. Setting Values: You can set the value of a mapping by using the key as an index. For example, balances[msg.sender] = 100; assigns the value 100 to the mapping balances at the key msg.sender. The key could be an address, a string, or any other data type that is allowed as a key.

  3. Accessing Values: You can retrieve the value associated with a key from a mapping using the key as an index. For example, uint myBalance = balances[msg.sender]; assigns the value of balances at the key msg.sender to the variable myBalance. If the key doesn't exist in the mapping, it will return the default value for that data type (e.g., 0 for uint).

  4. Updating Values: You can update the value associated with a key by assigning a new value to that key in the mapping. For example, balances[msg.sender] = 200; updates the value of balances at the key msg.sender to 200.

  5. Removing Values: Solidity doesn't have a built-in way to remove a key-value pair from a mapping. However, you can set the value to a default or empty value to effectively "delete" the key. For example, balances[msg.sender] = 0; sets the value of balances at the key msg.sender to 0, effectively removing the key-value pair.


Example of a Solidity smart contract that demonstrates how mappings work, including simple and nested mappings, and how to set, get, and delete values;

In this example, we have a smart contract called MappingExample that showcases the usage of mappings, including simple and nested mappings.

  • The contract includes a simple mapping balances, which maps addresses to unsigned integer values.

  • There is also a nested mapping nestedMapping, which maps addresses to another mapping, where each inner mapping maps unsigned integer keys to string values.

The contract provides the following functions:

  • setSimpleMappingValue(uint _value): Sets a value in the simple mapping balances for the caller's address.

  • getSimpleMappingValue(address _address): Retrieves the value from the simple mapping balances using the provided address.

  • deleteSimpleMappingValue(): Deletes the value from the simple mapping balances for the caller's address.

  • setNestedMappingValue(uint _key, string memory _value): Sets a value in the nested mapping nestedMapping for the caller's address and the provided key.

  • getNestedMappingValue(address _address, uint _key): Retrieves the value from the nested mapping nestedMapping using the provided address and key.

  • deleteNestedMappingValue(uint _key): Deletes the value from the nested mapping nestedMapping for the caller's address and the provided key.

By deploying and interacting with this contract, you can observe how to declare, set, get, and delete values in both simple and nested mappings.


Check out my other articles on Solidity Basics (Solidity Data Types and Operators)**, [solidity inheritance](favourajaye.hashnode.dev/solidity-inheritance), Solidity fallback function and function overloading, Variables and control structures in solidity, Solidity Functions, Libraries in solidity, Abstract contracts and Interfaces in solidity, [Guidelines on becoming a Blockchain Developer in 2023 (Solidity)](medium.com/coinsbench/guidelines-on-becomin..), [What is blockchain?](medium.com/web3-magazine/what-is-blockchain..), [All you need to know about web 3.0](medium.com/web3-magazine/all-you-need-to-kn..), [Solidity: Floating points and precision](medium.com/@favoriteblockchain/solidity-flo..), and others

Click here to see the Github repo for this 100 days of solidity challenge.

Don’t forget to follow me, put your thoughts in the comment section, and turn on your notifications.