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
aqeel ahmadaqeel ahmad 

Map<integer,List<String>> = new Map<integer,List<string>>(); how to put the list against each key..

Best Answer chosen by aqeel ahmad
Abhishek_DEOAbhishek_DEO
Since, you have "list" against key, you should use first "put" method to add first element in result and then "add" method to add subsequent values in list. a sample code would like like
 
Map<integer,List<String>> mapOfIntToString= new Map<integer,List<string>>(); 
for(integer i=0;i<10;i++){
 if(!mapOfIntToString.containsKey(i))
    mapOfIntToString.put(i,new List<String>{'test'});
 else
    mapOfIntToString.get(i).add('test1');
}


Please let me know if it helps.

Thanks,

Abhishek

All Answers

Abhishek_DEOAbhishek_DEO
Since, you have "list" against key, you should use first "put" method to add first element in result and then "add" method to add subsequent values in list. a sample code would like like
 
Map<integer,List<String>> mapOfIntToString= new Map<integer,List<string>>(); 
for(integer i=0;i<10;i++){
 if(!mapOfIntToString.containsKey(i))
    mapOfIntToString.put(i,new List<String>{'test'});
 else
    mapOfIntToString.get(i).add('test1');
}


Please let me know if it helps.

Thanks,

Abhishek

This was selected as the best answer
aqeel ahmadaqeel ahmad
Thanks Abhishek