Map in apex salesforce

Map is a collection that is most useful while building complex logic in apex class or trigger, Specially while building trigger logic for bulkification. A map is basically a collection of key & value pairs where in each unique key maps to a single value. Keys and values can be any of below data type—
Primitive types like integer or string, collections, sObjects, user-defined types, and built-in Apex types.

To declare a map use keyword map as done in below example
 map<string,integer>countryCodeMap=New map<string,integer>()
above map is declared to store keys of type string and values of type integer.

Similar to lists and set you can directly populate map with key and values at the time of declaration as below,
Map<String, String> countryCodeMap = new Map<String, String>{'india' => 91, 'USA' => 1}; 
In the example above india and USA are the two keys and values 91 and 1 are their respective values.

In order to add key values we can also use map method provided by salesforce. In case of map we can use put method as below,
countryCodeMap.put('China',76) 
This adds new key china with a value 76.

Basically map has a keyset which is nothing but a set and values are nothing but a list. Key will always be unique and cannot be duplicated while values can be duplicate.

Let us see some important points on map

1. We can retrieve all the keys from map using keyset() method and it returns set hence the output of keyset() method should be stored in a set as below
Set<string> allcountiersSet = countryCodeMap.keyset()2. We can retrieve all the values from map using values() method and it return list hence the output of values() method should be stored in a list as below 
Set<integer>allCountryCodesList= countryCodeMap.values()

3. A map key can hold a null value and hence we need to take necessary care of putting null checks.

4. As discussed above key is always unique and cannot be duplicate hence if you add a key with a value wherein the key is already present in map then the existing values against the existing key is replaced.for example
countryCodeMap.put('mexico',7)
countryCodeMap.put('mexico',8)
 
This would mean key mexico has value 8 against it as 7 is overwritten by 8.

5. Multiple keys can have same value against it, as values can be duplicate.for example
countryCodeMap.put('canada',6)
countryCodeMap.put('canada',7)
 

6. You need to be very careful while having keys of type string, reason - string keys are case sensitive, Ignoring which can be disastrous.

7. Soql query can be stored directly in map of type (id,sobject) which gives sObject record against id of that record. example below
map<id,account> idaccountmap = map<id,account>(select id,name,accountsource from account limit 50) 
This gives account record against its id. This can be very useful  while building logic and also saves no of lines.

8. You can use get() method to get a value against a specific key in the map. for example
countryCodeMap.put('india',91)
countryCodeMap.put('Germany',7)

Using get method below
countryCodeMap.get('india')
would return 91 and countryCodeMap.get('india') would return 7

Set in apex salesforce

Collection Set is similar to List but with few differences. It is important to understand these difference as otherwise it can cause failure in the logic that you have written which may not be easily visible during unit testing.

Set unlike List is a collection of elements which are unordered that means you can’t access a set element at a specific index. You can only iterate over set elements.

Second important point related to set is that it does not store duplicate elements. List on the other hand can store duplicate elements.

Set can store below types in it-
Primitive types like integer,string etc or Collections or SObject or user defined types like wrapper class or even built in apex types.

Example- Set<Integer> intSet = New Set<integer>()
Set<Id> idSet = New Set<Id>()


New elements can be added to the set by using add() method or you can add elements directly at the time of declaration as below,
Set<integer> mySet = new Set<integer>{4, 6, 9};

Sets can be nested, that is you can have set of set and this can go up to total five levels. 
Eg Set<Set<Id>> nestedSet
Simialr to List salesforce provides various system methods to manipulate data in set. Below are some of the commonly used methods for set data manipulation.

    add(newelement)
    Adds a new element to the set.
    addAll(anotherList)
    Adds all of the elements in the provided list to the set.
    addAll(anotherSet)
    Adds all of the elements in the specified set to the set.
    clear()
    deletes all of the elements from the set.
    clone()
    Makes a duplicate copy of the set.

Collections in apex salesforce - List

Apex gives us three types of collections.
1. List
2. Set
3. Map

List and Set are almost similar with few differences that we will discuss in another article. Map is different from list and set but incorporates both in it. It consists of key and values. In this article lets discuss List and various points on List and how to use it while building our logic in apex.

1. Lists

A list is nothing but a collection of elements which are marked by indexes, it is similar to array that we have learned in other languages. 
List can store any of the below data types-
Primitive types like integers, strings or sObjects even user defined types like wrapper classes and also built in apex types. 

List are ordered collection of elements unlike set which is unordered.
Below are some points that needs to be considered while using lists-
1. The List collection indices begin at 0


2. Lists can be nested with one another and this can go upto total 5 levels.


3. To declare a list keyword 'List' is used for example as below,
    List<String> intlist = new List<String>();

 
4. there are various methods that salesforce provides to manipulate data in a list collection. As an example add() this is one of the methods that is extensively used. This method allows us to add new elements in the list. As an example below statement adds new integer to the list defined in point 3.
   intlist.add(4);
   intlist.add(7);

 
5. A SOQL query results needs to be collected in List. A SOQL query returns collection of sobject that needs to be stored in a list of that type, that is return type of soql is list unless limit 1 is used in which case sobject type can be sued. As an example below
 List<account> accList = [Select id,name from account where createddate = system.today()];

 
6. Lets see few other methods that can be used to manipulate data in list collection.
    You can use Clear() method to clear all the data present in list. for example intlist.clear();
   You can set data at any specific index by using set method. for example below set element at second position as 2
    intlist.set(1,2);   


7. List can store duplicate elements in it which is not allowed in case of Sets


8. We can iterate over all the elements in list using advanced for loop wherein we need not check for last index or increase the counter. We can simply iterate over it and everything is taken care of as below.
   for(integer in : intlist){
        system.debug('integer values is'+in);
   }


Next>> Apex Collection - Set