OSSolidity.006: Solidity Mappings — Key-Value Storage, Default Values and Presence Checks

Colored-pencil illustration of keys linked to storage cells beside the Ethereum symbol, with BitcoinVersus.tech branding at bottom left.

Open-Source Solidity Programming • Intermediate • Estimated study time: 35 minutes

Smart contracts often associate an account with a numerical value or status. Solidity mappings provide direct key-based access to persistent contract state. Unlike arrays, mappings do not expose a sequence of stored keys or a length. Review Solidity value types and data locations before exploring storage-backed collections.

Prerequisites and learning objectives

Understand addresses, unsigned integers, booleans, state variables and basic function declarations.

  • Declare a mapping and explain its storage behavior.
  • Read, update and reset values by key.
  • Distinguish a default value from explicit registration.
  • Explain why mappings cannot enumerate keys.
  • Test account-specific updates and recognize access-control and privacy boundaries.

Mapping declarations and persistent state

The declaration mapping(address => uint256) public count associates Ethereum addresses with unsigned 256-bit integers. The Solidity compiler generates a getter for a public state mapping; callers supply an address and receive the associated integer. A mapping exists in storage, not as an ordinary memory array. Its default behavior is defined in the Solidity language documentation. Reading an untouched integer key returns zero, while reading an untouched boolean key returns false. The contract need not physically allocate an entry for every possible address.

Smart Contract Programmer: Mapping | Solidity 0.8. Demonstrates mapping syntax, reads, writes, deletion and nested mappings. Watch on YouTube.

Presence and deletion

A stored zero and an untouched key have identical integer results. If registration matters independently of the count, maintain a separate boolean mapping. Assigning true records presence; resetting both values removes the logical registration. The delete operator restores the selected value to its type’s default rather than shifting other entries. Compare these persistent state operations with the earlier lesson on contract structure and state variables.

Practice: account-specific training registry

The example allows a caller to record a fictional count under their own address. The functions use msg.sender as the key instead of accepting an arbitrary account to modify. The public getters support inspection, but no deposits, transfers or real financial balances are involved. Review function visibility for the external function declarations.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract TrainingRegistry {
    mapping(address => uint256) public count;
    mapping(address => bool) public registered;
    function setCount(uint256 value) external {
        count[msg.sender] = value;
        registered[msg.sender] = true;
    }
    function clear() external {
        delete count[msg.sender];
        delete registered[msg.sender];
    }
}

Local verification exercises

In Remix, select compiler 0.8.26 and deploy in the simulated Remix VM. For account A, read count(A) and registered(A): expect zero and false. Call setCount(0), then expect zero and true. Call setCount(7), then expect seven and true. Switch to account B and verify its initial zero and false remain unchanged. Return to A and call clear; expect zero and false again. These are expected outcomes for local validation, not recorded deployment measurements.

Smart Contract Data Structures: Solidity Mappings Explained. Covers mapping declarations, nested mappings and iteration considerations. Watch on YouTube.

Enumeration, gas and privacy

Mappings expose no built-in key enumeration. A separate key array or event-based off-chain index can support discovery, but each introduces consistency and storage considerations. For on-chain arrays, avoid unbounded iteration in state-changing functions because growing workloads can exceed available gas. Solidity storage is publicly observable on public chains even when a variable is declared private. Never store secrets in contract state. The EVM fundamentals lesson introduces execution and gas. Use simulated accounts only; production contracts require independent review and security testing.

Answer key

  • Does a zero result establish that a key was never written? Answer: no.
  • What does delete count[A] do? Answer: resets the integer to zero.
  • Can mappings directly enumerate every key? Answer: no.
  • Why use msg.sender in setCount? Answer: the caller updates the entry associated with their own address.
  • Does private storage guarantee confidentiality? Answer: no.

Featured illustration: conceptual depiction of keys associated with smart-contract storage; not an actual EVM storage layout.

We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiative, please scan the QR code below.

Optional 5,000-satoshi contribution QR
Optional 5,000-satoshi contribution. Independently confirm the wallet address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb and payment amount before sending.

BitcoinVersus.tech is not a financial advisor. This media platform reports on financial subjects purely for informational purposes.

Leave a Reply