Play Games

Search This Blog

Sunday, September 6, 2020

How to loop through map in order to get each element key and value in Apex Salesforce - Salesforce Globe For You

 How to loop through map in order to get each element key and value in Apex Salesforce  - Salesforce Globe For You 

Solution:First get list of keys of a map using Keyset() and then iterate over each key and use map.get(Key) to get value.

Example:

Map<String,String> alphabetMap = new Map<string,String>();

alphabetMap.put('A','Apple');

alphabetMap.put('B','Ball');

alphabetMap.put('C','Cat');

//iterate the map to get each element key and value

for(String alphabet:alphabetMap.keySet()) {

    System.debug('Key:'+alphabet+' Value:'+alphabetMap.get(alphabet));

}

Output:



No comments:

Post a Comment