What if you could instantly find any user's balance without searching through a long list every time?
Why Mappings in Blockchain / Solidity? - Purpose & Use Cases
Imagine you are managing a list of users and their balances on a blockchain without any direct way to link a user to their balance. You have to search through a long list every time you want to find or update a user's balance.
This manual search is slow and costly because every lookup requires scanning through all entries. It also increases the chance of mistakes, like updating the wrong user's balance or missing a user entirely.
Mappings provide a simple and efficient way to connect a unique key, like a user address, directly to a value, like their balance. This makes lookups and updates instant and reliable.
struct User { address addr; uint balance; }
User[] users;
function getBalance(address user) public view returns (uint) {
for (uint i = 0; i < users.length; i++) {
if (users[i].addr == user) return users[i].balance;
}
return 0;
}mapping(address => uint) balances;
function getBalance(address user) public view returns (uint) {
return balances[user];
}Mappings enable fast, direct access to data by key, making blockchain contracts more efficient and secure.
In a cryptocurrency wallet contract, mappings let you quickly find how many tokens each user owns without searching through all users.
Manual searching through lists is slow and error-prone.
Mappings link keys directly to values for instant access.
This makes blockchain contracts faster and safer.
Practice
mapping in blockchain smart contracts?Solution
Step 1: Understand the role of mapping
Mappings are used to link a key to a value, allowing quick access to data based on the key.Step 2: Compare options
Only To connect keys to values for fast data lookup correctly describes this purpose; others describe unrelated functions.Final Answer:
To connect keys to values for fast data lookup -> Option DQuick Check:
Mapping = key-value fast lookup [OK]
- Thinking mappings store all keys explicitly
- Confusing mappings with arrays or lists
- Assuming mappings perform calculations
address to uint in Solidity?Solution
Step 1: Recall Solidity mapping syntax
The correct syntax uses parentheses with key type, arrow, and value type: mapping(keyType => valueType) visibility name;Step 2: Check each option
mapping(address => uint) public balances; matches the correct syntax. Options A, C, and D have syntax errors like using commas instead of =>, wrong brackets, or function-like declaration.Final Answer:
mapping(address => uint) public balances; -> Option BQuick Check:
Correct mapping syntax uses (key => value) [OK]
- Using square brackets instead of parentheses
- Missing visibility keyword like public
- Using commas instead of =>
mapping(address => uint) public balances;
function setBalance(address user, uint amount) public {
balances[user] = amount;
}
function getBalance(address user) public view returns (uint) {
return balances[user];
}
// After calling setBalance(0x1234..., 50), what does getBalance(0x1234...) return?Solution
Step 1: Understand setBalance effect
The function sets the balance of the given address to the specified amount, here 50.Step 2: Understand getBalance output
Calling getBalance with the same address returns the stored value, which is 50.Final Answer:
50 -> Option AQuick Check:
Set then get returns stored value [OK]
- Assuming default zero is returned after setting
- Thinking mapping keys are enumerable
- Expecting an error for unset keys
mapping(address => uint) balances;
function addBalance(address user, uint amount) public {
balances[user] += amount;
}
function getBalance(address user) public view returns (uint) {
return balances[user];
}Solution
Step 1: Check mapping declaration
Mapping does not need explicit initialization; default values are zero.Step 2: Check function logic
addBalance adds amount to the current balance; getBalance returns it. Not declaring public means no automatic getter, but manual getter exists.Final Answer:
There is no error; the code works as expected -> Option CQuick Check:
Mapping defaults and manual getter work fine [OK]
- Assuming mapping must be public to read values
- Thinking mappings need explicit initialization
- Confusing mapping keys storage with values
Solution
Step 1: Understand ownership tracking needs
We need to check if a user owns a specific item ID, so a direct boolean check is best.Step 2: Analyze options
Only mapping(address => mapping(uint => bool)) ownsItem; // nested mapping user to item ID to ownership provides a nested mapping(address => mapping(uint => bool)) ownsItem, allowing direct ownsItem[user][itemID] lookup from the user address. A limits to one item per user; B requires starting from item ID; D requires searching an array.Final Answer:
mapping(address => mapping(uint => bool)) ownsItem; // nested mapping user to item ID to ownership -> Option AQuick Check:
Nested mapping allows direct ownership check [OK]
- Using single mapping limiting one item per user
- Using arrays which are not mappings
- Confusing item-to-owner with user-to-item ownership check
Ready to prove it?
Pass the test and this pattern moves unknown
