How to Add Elements to Map in apex

 Now I'm going to define a simple map and then add elements to that map. here I'm taking fruit's Names as values and the key as Serial Numbers.

Create and adding items to Map

Map <Integer, String> fruitsMap = new Map <Integer, String> ();
fruitsMap .put(1,'Apple');
fruitsMap .put(2,'Banana');
I can also add in one line like this.
Map <Integer, String> fruitsMap = new Map <Integer, String> {1=>'Apple',2=>'Banana'};

Labels:
Map
Join the conversation