In an array with key value pair; you can refer to a value using the corresponding key.
i.e. array[key]
will return the value
stored.
In solidity mapping, can be think of as an array and here in solidity syntax mapping (address => uint) public balances;
means that balances has set of uint
data type values that are mapped by address
data type.
Basically you can get a corresponding uint
value for a given address from balances
. To get a corresponding value here you need to call balances[address]
and that will return the uint
value.
You can find a detailed explanation in the solidity docs here.
Mapping types are declared as mapping(_KeyType => _ValueType)
. Here
_KeyType
can be almost any type except for a mapping, a dynamically sized array, a contract, an enum and a struct. _ValueType
can actually
be any type, including mappings.
Mappings can be seen as hash tables which are virtually initialized
such that every possible key exists and is mapped to a value whose
byte-representation is all zeros: a type’s default value. The
similarity ends here, though: The key data is not actually stored in a
mapping, only its keccak256
hash used to look up the value.
And this question will also be a good read on how mapping works.