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
Cristian TrifCristian Trif 

Code Snippet

Hello, I have a small code snippet here and i really don't know why i have this output:

User-added image
Best Answer chosen by Cristian Trif
GhanshyamChoudhariGhanshyamChoudhari
//    /*Salesforce1*/ this is comment
//lstString contains (Salesforce1,Salesforce2,Salesforce3)


For(String str/*Salesforce1*/ : lstString)
{
//in for loop we are dealing with 1st string which is Salesforce1 
// In mapOfString we  have ('key1','value1')
    if( mapOfString.containsKey(str /*Salesforce1*/) )
    {
	//if( mapOfString.containsKey(Salesforce1 )
	//mapOfString does not contains Salesforce1 it only contains key1 so this condition is false . so it will execute else BLOCK
        String existingvalue = 'test1';
                mapOfString.put(str,existingvalue);
    }
    else
    {
		//here str value is Salesforce1
        mapOfString.put(str,str);
		
    }
	//1st iteration is complete 
	//on 2nd iteration str value become Salesforce2 and so on

 

All Answers

GhanshyamChoudhariGhanshyamChoudhari
The output is correct.what you were expecting the output. so we can tinker code.
 
GhanshyamChoudhariGhanshyamChoudhari
//    /*Salesforce1*/ this is comment
//lstString contains (Salesforce1,Salesforce2,Salesforce3)


For(String str/*Salesforce1*/ : lstString)
{
//in for loop we are dealing with 1st string which is Salesforce1 
// In mapOfString we  have ('key1','value1')
    if( mapOfString.containsKey(str /*Salesforce1*/) )
    {
	//if( mapOfString.containsKey(Salesforce1 )
	//mapOfString does not contains Salesforce1 it only contains key1 so this condition is false . so it will execute else BLOCK
        String existingvalue = 'test1';
                mapOfString.put(str,existingvalue);
    }
    else
    {
		//here str value is Salesforce1
        mapOfString.put(str,str);
		
    }
	//1st iteration is complete 
	//on 2nd iteration str value become Salesforce2 and so on

 
This was selected as the best answer