# How many key-value entries can be stored in Solidity Mapping data structure?

``````mapping(uint => uint) public myMap;

function store(uint key, uint val) public {
myMap[key]=val;
}

function retrieve(uint key) public view returns (uint){
return myMap[key];
}
``````

Considering the above code, if we assume the size of uint is 256 bits(or 32 bytes), then can we store all possible 2^256 entries as key values in the mapping? each of these 2^256 can hold a uint value. So, the total space requirement to store the entire mapping data structure is (2^256)*32 bytes. Right? Since the state variables are stored in the blockchain itself, how it is possible to allocate and store such a huge memory in blockchain? What is the mechanism?

I have read an article, where it says for storage type state variables like mapping, we can populate a single entry (i.e. key-value pair). Only the caller need to pay the gas cost associated with a single entry writing. It also takes memory space for a single entry in the BC.

So, if that is the case- Can't I populate a mapping data structure using a loop(say, 10 lakhs entries in one go)? because then it consumes a huge gas cost? right? for e.g. the below code will not run due to excessive gas cost.

for(uint i=0;i<1000000;i++) { myMap[i]=2*i; }

*How many entries can be populated at max in a single go using a loop?

But if I can populate a single entry every time(by calling the store function, mentioned above) and calling this function for 10 lakhs times, hopefully, this will work. Isn't it?

• You can store as much as you want in a mapping, as long you pay gas for it. Each store is going to cost 20,000 gas. You can compute what would be a practical number that you would want to store. Also note that `2**256` is an obscenely large number. Even theoretically, you would have issues with `keccak256` hash collision invalidating data in your mapping for such large numbers. – hrkrshnn Feb 3 at 21:34