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
Hitesh chaudhariHitesh chaudhari 

how to iterate over map

I am having a map<Custom Object , String> type .

I want to iterate over  it so that in a single loop i will get output like

System.debug(key,value);
Best Answer chosen by Hitesh chaudhari
Harsh Chandra 23Harsh Chandra 23
Hi Nick,

You can iterate using KeySet() function:
 
for(Custom_Object__c objVar : map.keySet())
{
system.debug('Key = '+objVar+' Value = '+map.get(objVar));
}

Thanks,
Harsh

All Answers

Hitesh chaudhariHitesh chaudhari
Following is the Code  I am refering , which is not working

Map< BMCServiceDesk__BMC_BaseElement__c , String> mymap= new Map< BMCServiceDesk__BMC_BaseElement__c , String>();
               
for (Id id : mymap.keySet())
                {
                    System.debug(id);
                    System.debug(mymap.get(id));  // errror at this line
                } 
Harsh Chandra 23Harsh Chandra 23
Hi Nick,

You can iterate using KeySet() function:
 
for(Custom_Object__c objVar : map.keySet())
{
system.debug('Key = '+objVar+' Value = '+map.get(objVar));
}

Thanks,
Harsh
This was selected as the best answer
Hitesh chaudhariHitesh chaudhari
Thanks it works for me