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 of Map<ID,List<String>>

how to iterate over map of Map<ID,List<String>>in apex
Best Answer chosen by Hitesh chaudhari
GauravGargGauravGarg
Hi Nick,

As you are using List inside the Map, that means you need to loop over two variables like below:
Map<Id, list<String>> idToStringMap = new Map<Id, List<String>>();

for(Id idValue : idToStringMap.keyset())
{
   for(String s : idToStringMap.get(idValue ))
    {
        system.debug(' Map keyset Id Value' + idValue );
        system.debug(' list values : '+ s);
     }
}


Let me know if you have more question on it.

Thanks,

Gaurav
skype: gaurav62990

All Answers

GauravGargGauravGarg
Hi Nick,

As you are using List inside the Map, that means you need to loop over two variables like below:
Map<Id, list<String>> idToStringMap = new Map<Id, List<String>>();

for(Id idValue : idToStringMap.keyset())
{
   for(String s : idToStringMap.get(idValue ))
    {
        system.debug(' Map keyset Id Value' + idValue );
        system.debug(' list values : '+ s);
     }
}


Let me know if you have more question on it.

Thanks,

Gaurav
skype: gaurav62990

This was selected as the best answer
Hitesh chaudhariHitesh chaudhari
Map<ID,List<String>> testmap = new Map<ID,List<String>> ();

for (Id id : testmap .keySet())
            {
                System.debug('Key is '+ id);
                System.debug('this prints whole related list '+testmap .get(id));
                for(String s : testmap .get(id))
                {
                    System.debug('this prints list of string with each value separated  '+s);
                }
            }

 
SharndeepkaurSharndeepkaur
is it possible to do this without  double for loop?