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
s_macs_mac 

Map methods in apex

I have a map<String,String>

I have a key,value like salesforce=>Developer in my map,
Now i have to check for the key in my map and append value to my existing value to a particular key,like
Salesforce=>Developer,Adminstrator
if(map.containsKey(''Salesforce){
  get the value for the key and append a new value to the existing value
}
else{
insert new values to the map
}

Thanks
Amit Chaudhary 8Amit Chaudhary 8
Try below sample code
map<String,String> mapOfString = new map<String,String>();

List<String> lstString = new List<String>();
lstString.add('Salesforce');
lstString.add('A');
lstString.add('Salesforce');


For(String str : lstString)
{
	if( mapOfString.containsKey(str) )
	{
		String existingvalue = mapOfString.get(str);
		existingvalue= existingvalue+str;
                System.debug('---->'+existingvalue);
                mapOfString.put(str,existingvalue);
	}
	else
	{
		mapOfString.put(str,str);
	}
}	
System.debug('------mapOfString----->'+mapOfString);

Let us know if this will help you
 
VineetKumarVineetKumar
I suppose you are looking for something like
Map<String. List<String>>
for that refer the below code:
Map<String, List<String>> mapOfList = new Map<String, List<String>>();
if(mapOfList.containsKey(theKey)){
    mapOfList.get(thekey).add(values);
}else{
    mapOfList.put(thekey, new List<String>{value});
}
Let me know if this helped.
farukh sk hdfarukh sk hd
Hi,

These posts will cover about list,set,map usage and methods available and how to use map in lightning components,

https://www.sfdc-lightning.com/2018/09/collection-in-salesforce.html

https://www.sfdc-lightning.com/2018/09/how-to-use-map-in-lightning-component.html
Suraj Tripathi 47Suraj Tripathi 47
Hi s_mac,

"Try this code."
Map<String, List<String>> stringvsStringListMap= new Map<String, List<String>>();
if(stringvsStringListMap.containsKey(Key)){
    stringvsStringListMap.get(key).add(value);
}else{
    stringvsStringListMap.put(key, new List<String>{value});
}

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi