A mapping is a hash table with every possible key mapped to an instance of an element.
Unlike arrays, this means you can't generate an error by referencing an element that doesn't exist, because they all exist. If nothing was written to a given slot then it will return a zero-ish instance (
false, 0, empty, 0x0).
Also unlike arrays, it's not possible to iterate the keys or find out how many keys exist, because they all exist.
An important advantage of a
mapping is it's ability to access an element by an indentifier in one operation (
O(1)). This is very important for smart contracts.
mapping(address => struct) myMap; // id => struct
So, mappings are very useful, but sometimes we also want to be able to iterate the keys or count them. A common pattern is to use an array to hold the list of mappings that exist.
Have a look at "Mapped Struct with Index" over here: Are there well-solved and simple storage patterns for Solidity?
Hope it helps.