Imagine Map collection to be a google map. You enter a location name or zip code to get the entire information about the area in the google map.
Consider location name to be the KEY and information you get to be a VALUE.
Map works on same concept. It contains KEY and VALUE
Map<key, value> ();
KEY can be of any data type, but it is usually STRING or IDs
VALUE can be of any data type, but it is usually SObjects in salesforce
Declaring Map
Map<Integer, string> m1 = new Map<Integer, String> ();
Storing value into MAP: put(key,value)
m1.put(1,’Hello’);
m1.put(2.’world’);
Get Key from the MAP: keyset()
Set<Integer> allkeys = new Set<Integer>();
Allkeys = m1.keyset();
Get values from the MAP:
- values() returns all the values
List<String> values = new List<String>();
Values = m1.values();
- get(Key)
m1.get(1);
m2.get(2);
You can populate Key value by declaring curly brace {}. Inside Curly brace define Key followed by => and then value
Map<String, String> allstr = new Map<String, String>{1=>’a’, 2=>’b’};
We will cover advanced level of Map in another section . Please click on the link to check out advanced section of Map