Create Dictionary using map and list in Apex

Scenario: Create a real word dictionary showing multiple words on different pages.

With this use case you will be able to learn the nested usage of map and list.

1.I will create a map (wordsInDictionary) and put words in key and page number in the value. The question here is why will I add Words in key of the map? For this imagine a real dictionary, you will never see a word repeating in it. Also, if you remember in the map key is unique. Therefore the words will be added in the key and page number in the value.

wordDictionary.put('Apple', 1);

2. Now I want to see the multiple words on different pages of the dictionary. So for this I will create another map and name it (pageToWordMap). In that map I will pass PageNumber as key and List of words as value.

Again the question is why I choose page number as the key? The reason is page number is also unique i.e one page number only comes once in the dictionary. so the first map (wordsInDictionary) value can become key for the second map (pageToWordMap).

Second question is why I passed List<String> in the value? The reason is I want to display number of words on a particular page. So now I have an empty map having key as integer and List<String> as value.

Map<Integer, List<String>> pageToWordMap = new Map<Integer, List<String>>();

3. Now I will iterate over the keys of first map (wordsInDictionary) by using keyset()

for(String curWord: wordDictionary.keySet())

4.Get the value i.e. page number from the first map(wordsInDictionary) and store it it integer curPage

    Integer curPage = wordDictionary.get(curWord);

5. Now check the condition i.e. if page number does not already exist in the map (pageToWordMap) then only go inside if condition and add page number in the key of new map and new list of string in the value

        pageToWordMap.put(curPage, new List<String>());

6. Get the page number which is stored in curPage. Add the word which is stored in curWord because of iteration over the wordsInDictionary.keysey().

    pageToWordMap.get(curPage).add(curWord);

Below is the complete code.

//create a new map
Map<String, Integer> wordsInDictionary= new Map<String, Integer>();

//Add words and associated page number in the dictionary
wordsInDictionary.put('Apple', 1);
wordsInDictionary.put('World', 50);
wordsInDictionary.put('Watch', 50);
wordsInDictionary.put('Apex', 1);
wordsInDictionary.put('salesforce', 15);
 
//create a new map storing integer as key and List<String> as value
Map<Integer, List<String>> pageToWordMap = new Map<Integer, List<String>>();

//iterate over the keyset of the first map
for(String curWord: wordsInDictionary.keySet())
{

    Integer curPage = wordsInDictionary.get(curWord);
}

//if the new map does not contain that page number then go inside 
    if(!pageToWordMap.containsKey(curPage)) 
    {
       //add page number and new list of string only if map does not contain that page number
        pageToWordMap.put(curPage, new List<String>()); 
    }
/// get the page number and add the word
    pageToWordMap.get(curPage).add(curWord); 
}
 
for(Integer curPage: pageToWordMap.keySet())
{
    System.debug('Page ' + curPage + ' contains:');
}

Output

Complete grip of nested usage of map and list will help in tacking many complicated scenarios in apex. So go ahead and try this code in the developer console. Also practice some more scenarios involving list and map.

I hope you like this article. Stay tuned for reading more of these kind of articles. Happy Coding!

Reference

Map Class

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes