Mapping in Solidity Explained

Mapping in Solidity Explained

Table of contents

No heading

No headings in the article.

Mapping is identical to a dictionary in every other language.

In Solidity, it is a hash table that is used to store information in the form of key-value pairs, where the key can be any of the built-in data types (other than reference types), and the value can be any type.

But what exactly do we mean by "data types" in solidity?

As a first step, you should know that solidity is a statically typed language, which requires you to explicitly state the type of every variable you use. Data types allow the compiler to check the correct usage of the variables.

Likewise other statically typed languages Solidity has value types and reference types which are defined below:

Value Types Data is persistent in value-type variables. You can think of these as the bare bones of solidity's data structure. These types of variables are always passed by value. The variables are copied wherever they are used in function arguments or assignment.

( Boolean | Integer | Fixed Point Numbers | Address | Bytes & Strings | Enums )

In the below example, the contract Types initializes the values of different types of Values Types:

// Solidity program to demonstrate
// value types
pragma solidity ^ 0.5.0;

// Creating a contract
contract Types {

    // Initializing Bool variable
    bool public boolean = false;

    // Initializing Integer variable
    int32 public int_var = -60313;

    // Initializing String variable
    string public str = "GeeksforGeeks";

    // Initializing Byte variable
    bytes1 public b = "a";

    // Defining an enumerator
    enum my_enum { geeks_, _for, _geeks }

    // Defining a function to return
    // values stored in an enumerator
    function Enum() public pure returns(
    my_enum) {
        return my_enum._geeks;
    }
}

Output:

img128.png

Reference Types Reference type variables store the location of the data. They don’t share the data directly. With the help of reference type, two different variables can refer to the same location where any change in one variable can affect the other one.

( Arrays | Struct | Mapping ) In the below example, the contract Types initializes the values of various Reference Types.

// Solidity program to demonstrate
// Reference Types
pragma solidity ^0.4.18;

// Creating a contract
contract mapping_example {

    // Defining an array
    uint[5] public array
    = [uint(1), 2, 3, 4, 5] ;

    // Defining a Structure
    struct student {
        string name;
        string subject;
        uint8 marks;
    }

    // Creating a structure object
    student public std1;

    // Defining a function to return
    // values of the elements of the structure
    function structure() public view returns(
    string memory, string memory, uint){
        std1.name = "John";
        std1.subject = "Chemistry";
        std1.marks = 88;
        return (
        std1.name, std1.subject, std1.marks);
    }

    // Creating a mapping
    mapping (address => student) result;
    address[] student_result;
}

Output:

img224.png

Following a quick review of data types, let's continue on mapping. Syntax of Mapping:

mapping(key => value) <access specifier> <name>;

You can also use nested mapping in the following way:

mapping(_KeyType => mapping(_KeyType => _ValueType)) public mappingName;

How does mapping work?

Below is a list of room numbers and the names of those staying in them. This table can be viewed as a key-value pair in which each set of "keys" corresponds to a different value.

Therefore, the answer to the question "What does 102 mean?" is "Sara."

image.png

How to create mapping?

pragma solidity ^0.8.4;

contract MyContract {

    mapping(uint => string) public names;
}

You need to declare mapping with the mapping keyword, and then specify the data type for the key and the value. In this case, each key in the mapping will be a uint, and each corresponding value will be a string.

Before we deploy our smart contract to see how mapping works we add some data. To do that I will use the constructor function.

constructor() public {
        names[101] = "Joe";
        names[102] = "Sara";
        names[103] = "Ahmad";
        names[104] = "Rand";

    }
}

Now you can deploy the simple smart contract in Remix to see how it works. Once the contract is deployed you can see mapping in action. To do that you need to provide a key and click on “call”.

Screen Shot 2022-06-30 at 11.01.23 PM.png

In this example, we asked for value for key “101” and received “Jon”.

We've only scratched the surface of what mapping can do in solidity, but hopefully, this primer has been helpful. More applications of mapping will be discussed in subsequent articles.

References:

hackernoon.com/how-to-create-a-mapping-in-s..

docs.soliditylang.org/en/v0.8.4/types.html#..

geeksforgeeks.org/solidity-types