function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vepsun VarunVepsun Varun 

hi, i have 3 maps , map1 have 2 elements(1 'abc',2 'bcd'), map2 have 4 elements (1 'abc',2 'bcd',3 'cde',4'def') and map3 (0), ineed to add elememt in map3 which are not in map1 ?

please help me

map<integer,string> amap = new map<integer,string>();
        amap.put(1,'abc');
        amap.put(2,'bcd');

   map<integer,string> amap1 = new map<integer,string>();
        amap1.put(1,'abc');
        amap1.put(2,'bcd');
        amap1.put(3,'cde'); // i need to add this 3,4 to amap2
        amap1.put(4,'def');
   map<integer,string> amap2 = new map<integer,string>();

   for(integer setid:amap){
    if(!amap1.containskey(setid)){
       
    }
    }
   system.debug(setid);
Shruthi ChandrasekaranShruthi Chandrasekaran
Change your for loop as below which will set the 3rd map.
for(Integer i : amap1.keySet()){
    if(!amap.containsKey(i)){
        amap2.put(i,amap1.get(i));
    }
}
System.Debug(amap2);