Mapping
Mapping in Solidity is a hash table storing key-value pairs, linking unique Ethereum addresses to corresponding value types, similar to dictionaries in other languages.
Any other variable type that accepts a key type and a value type is referred to as mapping.
Syntax
mapping(key => value) <access specifier> <name>;
You can think of mappings as hash tables, Hash tables initialize keys to default values, with zeros representing byte-representations.
- _KeyType − can be any built-in types plus bytes and string. No reference type or complex objects are allowed.
- _ValueType − can be any type.
Please Note
Mapping can only have type of storage and are generally used for state variables.
Mapping can be marked public. Solidity automatically create getter for it.
Question / Answer
Mapping in Solidity acts like a hash table or dictionary in any other language. These are used to store the data in the form of key-value pairs, a key can be any of the built-in data types but reference types are not allowed while the value can be of any type.
Bibliography/References
- None