DEV Community

Cover image for Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts
Shlok Kumar
Shlok Kumar

Posted on

Unleashing the Power of Mapping in Solidity: Simplifying Complexity in Ethereum Contracts

Mapping in Solidity allows for efficient storage of key-value pairs in smart contracts.

Mapping in Solidity is a key-value data structure that is used to store information within a smart contract. It is similar to JavaScript objects or dictionaries in other programming languages. Mapping allows for the storage of values, where each value is associated with a unique key, making it easier to access and retrieve information stored within the smart contract.

Mapping in Solidity can be declared as a global state variable, and the key and value data types can be any elementary data type, including addresses and user-defined data types. The size of the mapping can be dynamic, allowing it to grow or shrink as the need arises. Mapping values can be accessed and updated directly, providing a flexible and efficient way to manage the data stored within a smart contract.

Additionally, mapping values can be iterated over, making it possible to retrieve information about all of the keys and values stored in the mapping. Overall, mapping is a powerful and versatile tool for managing data within a Solidity smart contract, and is a key component for many decentralized applications.

Mapping is like a hash table or a dictionary in any other language when you use it in Solidity . A key can be one of any of the built-in types, but reference types aren't allowed. The value can be any type, too. Mappings are mostly used to link an Ethereum address to a value type.

 

Syntax:

mapping(key => value) <access specifier> <name>; 
Enter fullscreen mode Exit fullscreen mode

Creating a Mapping:

Mapping is defined as any other variable type, which accepts a key type and a value type.

Example 1

// Solidity program to 
// demonstrate mapping
pragma solidity ^0.8.0; 
   
// Defining contract 
contract mapping_example {
      
    //Defining structure
    struct student 
    {
        // Declaring different 
        // structure elements
        string name;
        string subject;
        uint8 marks;
    }
Enter fullscreen mode Exit fullscreen mode


      

 

Example 2: Mapping and nested mapping

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Mapping {
    // Mapping from address to uint
    mapping(address => uint) public myMap;

    function get(address addr) public returns (uint) {
        // Mapping always returns a value.
        // If the value was never set, it will return the default value.
        return myMap[addr];
    }

    function set(address addr, uint i) public {
        // Update the value at this address
        myMap[_addr] = i;
    }

    function remove(address addr) public {
        // Reset the value to the default value.
        delete myMap[_addr];
    }
}

contract NestedMapping {
    // Nested mapping (mapping from address to another mapping)
    mapping(address => mapping(uint => bool)) public nested;

    function get(address addr1, uint i) public returns (bool) {
        // You can get values from a nested mapping
        // even when it is not initialized
        return nested[_addr1][_i];
    }

    function set(
        address addr1,
        uint i,
        bool boo
    ) public {
        nested[addr1][_i] = boo;
    }

    function remove(address addr1, uint i) public {
        delete nested[addr1][_i];
    }
} 
Enter fullscreen mode Exit fullscreen mode

For more content, follow me on - https://linktr.ee/shlokkumar2303

Top comments (2)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
shlok2740 profile image
Shlok Kumar

Thanks for the comment