Working with mappings
Mappings are one of the most widely used complex data types in Solidity. Mappings are similar to hash tables or dictionaries in other languages. They help in storing key-value pairs and enable the retrieval of values based on a supplied key.
Mappings are declared using the mapping
keyword, followed by data types for both keys and values separated by the =>
notation. Mappings have identifiers, as with any other data type, and they can be used to access the mapping.
An example of a mapping is as follows:
Mapping ( uint => address ) Names ;
In the preceding code, the uint
data type is used for storing the keys, and the address
data type is used for storing the values. Names
is used as an identifier for the mapping.
Although it is similar to a hash table and dictionary, Solidity does not allow iteration through mapping. A value from mapping can be retrieved if the key is known. The next example illustrates working with mapping. A counter of the...