This post has been updated click here
A map is like a hash table that allows you to save data on the Ethereum block chain in a key -> value pair relationship.
- It allows you to save data to the block chain
- You can retrieve this data in a function
- Maps are efficient for fast lookups
- You cannot get the size of a map
- Valid key types are string or byte which translates to a uint, bool and address
- Valid value types are any type
Use a map to keep track of data in one to one or one to many relationships.
Below is a sample of a solidity map in a smart contract.
pragma solidity >=0.7.0;
contract example {
//new state variable called myExampleMap
mapping (address => uint) public myExampleMap;
//To read an item in the map you use the get function
Function get(address _addr) public call returns (uint) {
Return myExampleMap[_addr];
}
//To set an item in the map you use the set function
Function set(address _addr, uint i) public call returns (uint) {
myExampleMap[_addr] = i;
}
//To remove an item in the map you use the remove function
Function remove(address _addr) public call returns (uint) {
Delete myExampleMap[_addr];
}
}
For more information read the docs.