paint-brush
How To Create a Mapping in Solidityby@kamilpolak
11,664 reads
11,664 reads

How To Create a Mapping in Solidity

by Kamil PolakMay 23rd, 2021
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

In Solidity when you want to store a collection of data you can use array or mapping. In this article, I will show you how to create a mapping and how to add keys and values using the constructor function. The constructor function allows you to add a key and a value using the same syntax as the array function. In the following article, we will deploy our simple smart contract in Remix to see how it works using mapping. You can think about this table as a key-value pair relationship where you can find a unique set of “keys” that correspond to a unique value.

Company Mentioned

Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - How To Create a Mapping in Solidity
Kamil Polak HackerNoon profile picture

In Solidity when you want to store a collection of data you can use array or mapping. In this article, I will show you how to create a mapping and how to add keys and values using the constructor function.

What is mapping? 

Following the Solidity documentationYou can think of mappings as hash tables, which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros, a type’s default value”

In other words, mapping in Solidity is like a dictionary in Python or a map in JavaScript. Python allows you to iterate through a dictionary and JavaScript allows you to iterate through the map. However, in Solidity, you cannot iterate through the mapping.

Mapping use the following syntax

mapping(_KeyType => _ValueType) public mappingName

You can also use nested mapping in the following way

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

Let me explain how mapping works. The table below presents the room numbers with corresponding hotel guest names. You can think about this table as a key-value pair relationship where you can find a unique set of “keys” that correspond to a unique value.

So if you ask for the value for “102” you will receive “Sara”.

Let’s see how to create a mapping:

pragma solidity ^0.8.4;

contract MyContract {
    
    mapping(uint => string) public names;
}

First, 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

contructor
function.

constructor() public {
        names[101] = "Jon";
        names[102] = "Sara";
        names[103] = "Paul";
    }
}

Now we can deploy our simple smart contract in Remix to see how it works. Let’s click on “Deploy”.

Once the contract is deployed you can see mapping in action. To do that you need to provide a key and click on “call”.

In my example, I asked for value for key “101” and received “Jon”.

Takeaways

  • mapping does not have a length,
  • mapping is allowed for state variables,
  • mapping cannot return parameters of contract functions that are publicly visible,
  • you cannot iterate through the mapping.

I hope this short tutorial helps you to understand the basics of mapping in Solidity. 

References:

https://docs.soliditylang.org/en/v0.8.4/types.html#mapping-types