SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex Collections Simplified

Apex Collections, as the name says its a collection. Collection of what? It’s a collection of similar Types.

It’s a Data Structure that allows you to keep a group of similar Types stored in a single variable which can be later used.

There are used extensively in Apex and you are going to use them like a pro 🙂

In Apex we have primarily 3 types available – List, Sets and Maps

We will cover them one by one.

List :

The list is similar to an array. What is an Array? An Array is a collection of similar Type elements. This is also true for List as well.

Declaring & Initialising A Blank List :

List<String> planetsList = new List<String>();

What you see as <String> is defining the Type for which this List is valid. If you try to add an Integer into the same list then Apex Compiler won’t be very comfortable and will raise some concern in the form of an error saying hey buddy you said you will add Strings only.

Here is what it will say in the error message – ‘Method does not exist or incorrect signature: void add(Integer) from the type List<String>’

Declaring & Initialising List With Members :

// Only Taking First Four Planets. Feel Free to Extend it in your code :)
List<String> planetsList = new List<String>{'Mercury', 'Venus', 'Earth', 'Mars'};

Getting Elements From List :

String firstPlanet = planetsList.get(0);

We use List.get(Integer Index) method to fetch an element at a given index. List first elements has a starting index as 0.

Adding Elements To List :

planetsList.add('Jupiter');

Removing An Element From List :

planetsList.remove(4);

Here in the remove() method, we are specifying the index location which we wanted to remove.

Iterating a List :

for (String planetName : planetsList) {
    System.debug('Planet ' + planetName);
}

Size Of List :

Integer planetCount = planetList.size();

Finding If List Is Empty :

Boolean isPlanetListEmpty = planetList.isEmpty();

You can also check by List.size() method as well if a list contains some elements or not but it’s a good practice to use List.isEmpty() method to check for List emptiness.

Clearing a List :

planetList.clear();

Adding a List To Another List :

List<List<String>> galaxyList = new List<List<String>>();
List<String> planetsListMilkyWay = new List<String>{'Mercury', 'Venus', 'Earth', 'Mars'};
galaxyList.add(planetsListMilkyWay);

Here we have specified the Type as List<String> because we wanted to store a list of String to a given list.

Set :

The Set can be said as a cousin of List. It also stores similar Types of elements but ensures that there are no duplicates as in the List. Also, be careful while using Sets as even though Apex is a case insensitive language but Set respects case sensitivity.

Declaring & Initialising A Blank Set :

Set<String> planetSet = new Set<String>();

Here also you can only add String Type of elements into this set.

Declaring & Initialising Set With Members :

Set<String> planetSet = new Set<String>{'Mercury', 'Venus', 'Earth', 'Mars'};

Getting Elements From Set :

You can’t get an element directly from a set as we did in List using the get() method. It is because the set is an unordered collection.

Adding Elements To Set :

planetSet.add('Jupiter');

Confirming If An Element Is Present In Set :

planetSet.contains('Jupiter');

Removing An Element From Set :

planetSet.remove('Jupiter');

Here in the remove() method, we are specifying the element which we wanted to remove.

Iterating a Set :

for (String planetName : planetSet) {
    System.debug('Planet ' + planetName);
}

Finding If Set Is Empty :

Boolean isPlanetSetEmpty = planetSet.isEmpty();

Clearing a Set :

planetSet.clear();

Adding a Set To Another Set :

Set<Set<String>> galaxySet = new Set<List<String>>();
Set<String> planetsSetMilkyWay = new Set<String>{'Mercury', 'Venus', 'Earth', 'Mars'};
galaxySet.add(planetsSetMilkyWay);

Here we have specified the Type as Set<String> because we wanted to store a set of String to a given set.

Map :

The Map can be said as another distant cousin of List living in another country. It also stores similar Types of elements in its own way.

At a time when we didn’t have google maps or GPS technology, there were Maps with coordinates that point to a specific geographical location. We can call those coordinates Keys that point to a specific location.

Similarly, Apex Map has a key that points to a specific value. Also, known as key-value pair.

Like Set, Map keys are case-sensitive.

Declaring & Initialising A Blank Map :

Map<Integer, String> planetMap = new Map<Integer, String>(); 

Here we have defined a key as an Integer and the value to be stored against each key as String. Here also you can only add String Type of elements into the value with a key of Integer.

Declaring & Initialising Map With Members :

Map<Integer, String> planetMap = new Map<Integer, String> {
    1 =>'Mercury', 
    2 =>'Venus',
    3 => 'Earth', 
    4 => 'Mars'
};

Getting Elements From Map :

planetMap.get(1);

We specify the key in the Map.get() method. It’s not the index location that we used in List.get() method.

Adding Elements To Map :

planetMap.put(5, 'Jupiter');

Here we are adding the key first and later its respective value in the Map.put() method

Confirming If A Key Is Present In Map :

Boolean ifKeyPresent = planetMap.containsKey(1);

Removing An Element From Map :

planetMap.remove(5);

Here in the remove() method, we are specifying the key which will remove the respective key-value pair from the Map.

Iterating a Map Keys:

for (Integer planetKey : planetMap.keySet()) {
    System.debug('Planet ' + planetKey);
}

Here in the dataType Declaration for the for loop, we have taken as Integer because the Map key was declared to be of Integer Type.

Iterating a Map Values:

for (String planet : planetMap.values()) {
    System.debug('Planet ' + planet);
}

Here in the dataType Declaration for the for loop, we have taken as String because the Map values were declared to be of String Type.

Finding If Map Is Empty :

Boolean isPlanetMapEmpty = planetMap.isEmpty();

Clearing a Map :

planetMap.clear();

Adding a Map To Another Map :

Map<Integer, String> planetMapMilkyWay = new Map<Integer, String> {
    1 =>'Mercury', 
    2 =>'Venus',
    3 => 'Earth', 
    4 => 'Mars'
};

Map<String, Map<Integer, String>> galaxyMap = new Map<String, Map<Integer, String>>();
galaxyMap.put('galaxy-1', planetMapMilkyWay);

Here we have specified the Type as Map<String, Map<Integer, String>> because we wanted to store a Map of <Integer, String> to a given Map with a key as String.

So by this line, you have covered a good amount of details regarding Apex Collections and it’s the right time for you to start using them in your projects like a pro.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


3 thoughts on “Apex Collections Simplified

  • With your post, your readers, particularly those beginners who are trying to explore this field won’t leave your page empty-handed. Here is mine at QH7 I am sure you’ll gain some useful information about Airport Transfer too.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *