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
Ashu sharma 38Ashu sharma 38 

how to merge two maps and create third map

Hi,

As I am creating two maps now  I want to create the third map in which i have to put the values from 1st and 2nd Map.

Thanks
Soyab HussainSoyab Hussain
Hi Ashu,

See this code example this will help you.
// first map
Map<String, String> firstMap = new Map<String, String>();
firstMap.put('Red','FF0000');
System.debug('firstMap' + firstMap);
// second map
Map<String, String> secondMap = new Map<String, String>();
secondMap.put('Blue','0000FF');
System.debug('secondMap' + secondMap);
//third map
Map<String, String> combinedMap = new Map<String, String>(); 
// combining maps
combinedMap.putAll(firstMap);
combinedMap.putAll(secondMap); 
System.debug('combinedMap' + combinedMap);


If you found it useful please appreciate my efforts and mark it as the best answer.

LinkedIn:  https://www.linkedin.com/in/soyab-hussain-b380b1194/

Regards 
Soyab
Ashu sharma 38Ashu sharma 38
Hi Soyab,

Thanks for reply..
As i need to change the key value pair from below code:

 map<id,set<id>> programTermAndprogramCodeMap=new map<id,set<id>>();
        for(Program_Session__c ps:programTermListWithProgramCode){
            set<id> programCodeProgramTerm=new set<id>();
            programCodeProgramTerm.add(ps.Program__r.Program_Code__c);
          // programCodeProgramTerm.add(ps.id);
            programTermAndprogramCodeMap.put(programCodeProgramTerm,ps.Id);
         //  programTermAndprogramCodeMap.put(ps.id,programCodeProgramTerm);   /*Program term and Program code */
            system.debug('programTermAndprogramCodeMap' +programTermAndprogramCodeMap);
        }

I need to change as Programe code(key) and Program term(value)..any suggestions