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
Rahul Chavan 36Rahul Chavan 36 

for loop

hey guys plz refer below code in which by using for loop and : this symbol we fetch maps key and value but i dont want to use it but with for loop how i acheive it.. as beginner plz help me out..

map<string,string> mapr = new map<string,string>();
  //   <key,value>
    mapr.put('one','first element');
    mapr.put('two','2nd element');
    mapr.put('three','3rd element');


//iterate over keyset

for(string str:mapr.Keyset()){
system.debug('Key=> '+str+'  Values=> '+mapr.get(str));
}


so without this(string str:mapr.Keyset()) method
how i acheived it in for loop to print maps key values and values?? 
ShivankurShivankur (Salesforce Developers) 
Hi Rahul,

Above is the only way to iterate over collection variable.

Please check out the Apex help documentation around it:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_lists.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for.htm

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.
Rahul Chavan 36Rahul Chavan 36
hey shivankur, thank u for reply, i know that apex developer guide but my issue how to achieve with traditional for loop to print keys and values of maps..