Mappings

Mappings are one of the most 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 retrieving values based on the supplied key.

Mappings are declared using the mapping keyword followed by data types for both key and value separated by the => notation. Mappings have identifiers like any other data type and they can be used to access the mapping.

An example of 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 ...

Get Solidity Programming Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.