Skip links

Table of Contents

Solidity Mappings Explained: A Beginner-Friendly Guide

TL;DR

Mappings in Solidity are key-value stores that offer fast lookups and efficient storage but lack iteration capabilities. They are ideal for storing balances, permissions, and relationships between addresses. Use mappings carefully, avoid unnecessary data storage, and be mindful of access control to optimize gas costs and security in smart contracts.

What is a Mapping in Solidity?

Solidity mapping

A mapping in Solidity is a key-value store that allows developers to associate unique keys with specific values. It functions similarly to a dictionary or a hash table in other programming languages.

Think of a mapping like a real-world address book, where a name (key) maps to a phone number (value). In Solidity, mappings are widely used for storing user balances, access control settings, and references to complex data structures.

Syntax of Mappings in Solidity

Mappings in Solidity are defined using the following syntax:

mapping(keyType => valueType) <visibility> <name>;
  • keyType – The data type of the key (e.g., address, uint, bytes32).
  • valueType – The data type of the value (e.g., uint, bool, struct).
  • visibility – Access specifier (public, internal, private).
  • name – The identifier of the mapping.

Example:

mapping(address => uint) public balances;

This creates a public mapping where Ethereum addresses (address) are linked to unsigned integers (uint) representing balances.

How to Use Mappings in Solidity

Solidity Mappings are essential for managing data storage efficiently in smart contracts. Here’s how you can use them:

1. Storing and Retrieving Data

A mapping allows direct storage and retrieval of values:

pragma solidity ^0.8.0;

contract SimpleBank {
   mapping(address => uint) public balances;

   function deposit() public payable {
       balances[msg.sender] += msg.value;
   }

   function withdraw(uint amount) public {
       require(balances[msg.sender] >= amount, "Insufficient balance");
       balances[msg.sender] -= amount;
       payable(msg.sender).transfer(amount);
   }

   function getBalance() public view returns (uint) {
       return balances[msg.sender];
   }
}

2. Nested Mappings

Mappings can also be nested to create more complex data structures:

mapping(address => mapping(address => uint)) public allowances;

This allows tracking of approvals where one address (owner) authorizes another address (spender) to use a certain amount of tokens.

Best Practices and Mistakes to Avoid with Mappings

Best PracticesCommon Mistakes
Use for Fast Lookups: Mappings provide quick access to values based on unique keys.Avoid Iteration: Mappings don’t support looping through stored keys.
Initialize Properly: Ensure mappings are set up correctly within the contract.Don’t Declare in Memory: Mappings can only be used in storage, not memory.
Manage Access Control: Use mappings to assign roles and permissions efficiently.Watch Storage Costs: Unnecessary data storage in mappings can lead to higher gas fees.
Combine with Structs: Mappings work well when paired with structs to store complex data.Be Cautious with Public Visibility: Exposing mappings publicly may leak sensitive data.

Additional Considerations

Mappings vs Arrays

  • Use mappings when looking up data via a unique key.
  • Use arrays when ordering and iteration are required.

Use Cases for Mappings

  • Token Balances: Tracking ERC-20 token balances per address.
  • Voting Systems: Storing voter choices in decentralized applications.
  • Access Control: Managing user roles and permissions.
  • Escrow Contracts: Handling deposits and transactions in smart contracts.

Mappings are a fundamental storage structure in Solidity, offering fast lookups and efficient storage. However, they come with limitations, such as the inability to iterate over keys. By following best practices and avoiding common pitfalls, developers can leverage mappings to create secure, efficient, and optimized smart contracts.

FAQs

What is a mapping in Solidity?

  • A mapping is a key-value storage system that allows quick lookups of values using unique keys, commonly used for tracking balances, permissions, and relationships between addresses.

Can I iterate over a mapping?

  • No, mappings do not support iteration because Solidity does not store keys in an iterable way. You need an additional array to track keys.

Can mappings be used in memory?

  • No, mappings can only be used in contract storage. They cannot be declared in memory or passed as function parameters.

Are mappings more efficient than arrays?

  • Yes, mappings allow constant-time lookups, making them more efficient than arrays when searching for specific values.

Can I delete an entry in a mapping?

  • Yes, but only by setting the value to its default (e.g., 0 for uint). Mappings do not have a built-in delete function.

Metana Guarantees a Job 💼

Plus Risk Free 2-Week Refund Policy ✨

You’re guaranteed a new job in web3—or you’ll get a full tuition refund. We also offer a hassle-free two-week refund policy. If you’re not satisfied with your purchase for any reason, you can request a refund, no questions asked.

Web3 Solidity Bootcamp

The most advanced Solidity curriculum on the internet!

Full Stack Web3 Beginner Bootcamp

Learn foundational principles while gaining hands-on experience with Ethereum, DeFi, and Solidity.

You may also like

Metana Guarantees a Job 💼

Plus Risk Free 2-Week Refund Policy

You’re guaranteed a new job in web3—or you’ll get a full tuition refund. We also offer a hassle-free two-week refund policy. If you're not satisfied with your purchase for any reason, you can request a refund, no questions asked.

Web3 Solidity Bootcamp

The most advanced Solidity curriculum on the internet

Full Stack Web3 Beginner Bootcamp

Learn foundational principles while gaining hands-on experience with Ethereum, DeFi, and Solidity.

Learn foundational principles while gaining hands-on experience with Ethereum, DeFi, and Solidity.

Events by Metana

Dive into the exciting world of Web3 with us as we explore cutting-edge technical topics, provide valuable insights into the job market landscape, and offer guidance on securing lucrative positions in Web3.

Subscribe to Lettercamp

We help you land your dream job! Subscribe to find out how

Fall In Love with your
career again!

Fall In Love with your career again! 20%OFF

20% oFF

Days
Hours
Minutes
Seconds

New Application Alert!

A user just applied for Metana Web3 Solidity Bootcamp. Start your application here : metana.io/apply

Get a detailed look at our Full Stack Bootcamp

Understand the goal of the bootcamp

Find out more about the course

Explore our methodology & what technologies we teach

You are downloading 2025 updated Full stack Bootcamp syllabus!

Download the syllabus to discover our Full-Stack Software Engineering Bootcamp curriculum, including key modules, project-based learning details, skill outcomes, and career support. Get a clear path to becoming a top developer.

Software Engineering Syllabus Download

"*" indicates required fields

This field is for validation purposes and should be left unchanged.