5

I tried to insert two entries of identical keys into a map for testing.

The map seems to reject the later entry and preserves the original entry.

Is this just Solidity behaving normally? (If it is not, then it means something is wrong with my code.)


I read somewhere that HashMaps are meant to overwrite any entries when inserting a duplicate key.

Are hash maps in solidity designed to handle data differently? What is it meant to do when encountering a duplicate key entry?

Solidity documentation doesn't seem to dig too deep into this.

1 Answer 1

5

In case the mapping already contains a value for a particular key it will be overwritten with the new value. Here is the test case:

  pragma solidity ^0.4.11;

  contract TestMapping {

      mapping(uint => uint) public testMapping;

      function testMapping() public returns (uint) {
          testMapping[1] = 10;
          testMapping[1] = 20;
          return testMapping[1];
      }
  }

The result is 20;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.