Solidity Tutorial: all about Mappings

Jean Cvllr
Coinmonks
8 min readAug 31, 2020

--

The School Cloackroom by Malcolm Evans, a cartoon drawing used as an analogy to explain mappings in Solidity (taken from https://www.cagle.com/tag/cloakroom/)

Table of Content

  • Introduction to mappings
  • What mappings are used for?
  • How mappings are represented in storage?
  • Key and Values types allowed
  • Operations on Mappings
  • Nested Mappings
  • Mappings as function parameters
  • Structs as value types of mappings
  • Mappings Limitations

Introduction to mappings

Mappings in Solidity are similar to the concept of hash map in Java or dictionary in C and Python. They act like hash tables, although they are slightly different.

There is nothing better than an analogy with the real world to understand what are mappings in Solidity and how they behave. The following example was taken from a Reddit post.

A hashmap (= mapping) is like a cloakroom. You hand your coat over and get a ticket. Whenever you give that ticket back, you immediately get your coat. You can have a lot of coats, but you still get your coat back immediately. There is a lot of magic going on inside…

--

--