Mapping Types
Mapping
mapping
can be used to create a hashmap
(similar to dict
in python
) between 1 type
to another type
.
Note
When you create a mapping
, all keys
exist at the same time. That means:
For example, you create mapping(address => uint256) addressToValue;
. If you haven't set any key
and value
then all address
that you input will return the default value of uint256
which is 0.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Mapping {
// Mapping from address to uint
mapping(address => uint) public myMap;
function get(address _addr) public view returns (uint) {
// Mapping always returns a value.
// If the value was never set, it will return the default value.
return myMap[_addr];
}
function set(address _addr, uint _i) public {
// Update the value at this address
myMap[_addr] = _i;
}
function remove(address _addr) public {
// Reset the value to the default value.
delete myMap[_addr];
}
}
contract NestedMapping {
// Nested mapping (mapping from address to another mapping)
mapping(address => mapping(uint => bool)) public nested;
function get(address _addr1, uint _i) public view returns (bool) {
// You can get values from a nested mapping
// even when it is not initialized
return nested[_addr1][_i];
}
function set(address _addr1, uint _i, bool _boo) public {
nested[_addr1][_i] = _boo;
}
function remove(address _addr1, uint _i) public {
delete nested[_addr1][_i];
}
}
Last updated