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 

loop

hey guys as beginner in apex   my reqmt to write ver basic apex code with for loop not which use in apex like chaeck condition directly with sign :
 so for map i want to print map1 key is one and value is first element
map1 key is two and value is 2nd element  and it goes on..
 i tried to build logic but it will not work , below is my logic code

   map<string,string> mapr = new map<string,string>();
    mapr.put('one','first element');
    mapr.put('two','2nd element');
    mapr.put('three','3rd element');
    
    system.debug('mapr= ' +mapr);
    
 integer maprsize = mapr.size();
set <string> maprkeys=mapr.keyset();
list<string> maprvalues=  mapr.values();
    ****for (string x<= maprkeys; x++){
        system.debug('for mapr key is '  + maprkeys + '  and its value is ' + maprvalues );
        system.debug(x);
                    
                 }
and signed star portion is error , so kindly help to get map keys and then by that keys we  print values .by standard for loop , not the one for loop which we use in apex ,below also i give the apex code for loop which work but i want to also work in standard for 

map<string,string> map1= new map<string,string>;
map1.put('one', 'first element');
map 1.put('two','2nd element');
system.debug('map= ' +map1);

for(string mapitem : map1.keyset() {
string tempstring = 'key is ' +map1item+ ' , and its value is ' +map1.get(map1item);
system.debug(tempstring);
}
as a beginner i know its basic but plz help me out with same simple code..
ravi soniravi soni
hy,
take referance from below code.
map<string,string> map1= new map<string,string>();
map1.put('one', 'first element');
map1.put('two','2nd element');
system.debug('map= ' +map1);

for(string mapitem : map1.keyset()) {
string tempstring = 'key is ' +mapitem + ' , and its value is ' +map1.get(mapitem);
system.debug(tempstring);
}
if it helps you, don't forget to mark it as best answer.
Thank you
 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

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));
	}
Thank You
Rahul Chavan 36Rahul Chavan 36
hey suraj and soni thank u for reply,
 but as i mentioned i do not want it by apex for loop and by : method which you used(
for(string mapitem : map1.keyset()) but with sttandard for loop without : this sybmol it will return key and its value hope  u answer it as well