Mappings in Solidity Explained in Under Two Minutes

Doug Crescenzi
May 23, 2018 · 3 min read

Mappings act as hash tables which consist of key types and corresponding value type pairs. They are defined like any other variable type in Solidity:

mapping(_KeyType => _ValueType) public mappingName
example of a hash table

Mappings are incredibly useful for associations and frequently used to associate unique Ethereum addresses with associated value types. For instance in the blockchain-based puzzle game we built it was necessary for us to map a user’s address to their corresponding level in the game:

mapping(address => uint) public userLevel;

With this mapping in place we then built a function to identify a user’s current level in the game. Here we pass the currentLevel function an argument — i.e., a particular user’s address — and then we use the userLevel mapping to return that user’s associated level in the game:

function currentLevel(address userAddress) public constant returns (uint) {     return userLevel[userAddress];}

Using a mapping here is helpful because it can store many _KeyTypes to _ValueTypes. In this case there are many users playing the game all at once and they can each have their own corresponding userLevel.

According to the Solidity docs, “mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation are all zeros.

Essentially this means that mappings do not have a length. They also do not have a concept of a key or a value being set. We can only use mappings for state variables that act as storage reference types. It’s then possible to create a getter function call (like public) in which the _KeyType is the parameter used by the getter function in order to return the _ValueType.

Let’s reflect back on our userLevel mapping from our game:

mapping(address => uint) public userLevel;

When userLevel is initialized it’s done in such a way that every possible Ethereum address exists in the mapping and is mapped to a corresponding level of 0. Whether it’s a random Ethereum user’s MetaMask address who perhaps has never even heard of our game before, or maybe an arbitrary smart contract on the Ethereum blockchain doing something totally unrelated, it doesn’t matter. Our mapping still maps them to a corresponding level of 0 in our game, or more appropriately, maps to a “value whose byte-representation are all zeros.”

  • Mappings act as hash tables which consist of key types and corresponding value type pairs
  • Mappings are useful because they can store many _KeyTypes to _ValueTypes
  • Mappings do not have a length, nor do they have a concept of a key or a value being set
  • Mappings can only be used for state variables that act as storage reference types
  • When mappings are initialized every possible key exists in the mappings and are mapped to values whose byte-representations are all zeros

We're a women-owned business that helps B2B organizations…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store