Skip to main content

Is there a way to add (mathematically) values from two maps together if the key is the same? 

3 answers
  1. Jan 12, 2022, 5:14 AM

    @Ash couple of things 

     

    Map cannot have 2 same keys, like Map1 cannot have a twice.

     

    second, i have tried second example here:

    Map<ID, Integer> map1 = new Map<ID, Integer>{'A' => 20, 'B' => 40, 'C' =>30};

    Map<ID, Integer> map2 = new Map<ID, Integer>{'A' => 30, 'B' => 50, 'C' =>50};

    Map<ID, Integer> map3 = new Map<ID, Integer>();

     

    for(string temp: map1.keyset()){

    map3.put(temp, (map1.get(temp)+map2.get(temp)))

    }

     

    May be this will work for you.

0/9000