SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Friday, 10 September 2021

List Data Structure in Apex | Apex Data Structure Tutorials by SFDC Stop

Hello Trailblazers,


In this post, we're going to learn about how we can use list data structure in apex. A list is a collection of elements or records that you want to store. List in apex can store elements of any data type. It can store: Integer, String, Boolean and any custom data type as well. For example: Account, Contact, Employee__c (Custom Object) etc.


The syntax to create a new list is as follows:

List<DataType> listName = new List<DataType>();


Here, DataType should be replaced by the type of data you would like to store and listName should be replaced by the name of the list. For example, if I want to create a list of integers, I can create it as follows:

List<Integer> numbers = new List<Integer>();

The above code snippet will create a list of Integers named as numbers. In this list you can store any number of integers. If you want to add elements to the list at the same moment when a list is initialized, you can do that as well. Consider the below examples:

1. Creating a list of Integers


Code Snippet:

List<Integer> numbers = new List<Integer>{1,2,3,4,5};
System.debug(numbers);


Output:

2. Creating a list of Strings


Code Snippet:

List<String> names = new List<String>{'Richard', 'Gilfoyle', 'Dinesh', 'Jared', 'Monica'};
System.debug(names);


Output:


In both the above examples, you can see that we've used curly braces {} to specify a comma separated set of values and that values are automatically added to the list. Using this way, you can initialize a list with a set of values. But what if you need to add some more data later on? Let's consider that now:

Adding elements to the list

You can use the add() method to add elements to the list. Let's consider the below example:

Code Snippet:
List<Integer> numbers = new List<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
System.debug(numbers);

Output:


As you can see above, first of all I initialized a list and then added 5 elements to the list one by one using add() method. Here also, our list is created successfully with 5 elements: 1, 2, 3, 4 and 5.

Accessing elements from the list

Each element in a list is associated with an index starting from 0, for example: the list above contains 5 elements which are stored in the memory as follows:
Let's say you want to access element at index 3, you can simply refer to that by: numbers[3] or you can also use the get() method as: numbers.get(3). Considering the above list, the element at index 3 is: 4. Let's try to code this and see the output:

Code Snippet:
List<Integer> numbers = new List<Integer>{1,2,3,4,5};
System.debug(numbers);
System.debug('Element at index 3 = ' + numbers[3]);
System.debug('Element at index 3 using get() = ' + numbers.get(3));

Output:

As you can see above, I am getting the correct value i.e. 4 in the debug while accessing the element at index 3 by square brackets [] as well as by using the get() method.

Adding element at a particular index

If you want to add element at a particular index, you can use the add() method to do so. Just pass the index as the first parameter and the value as the second parameter to the add() method. For example:


Code Snippet:

List<Integer> numbers = new List<Integer>{1,2,3,4,5};
System.debug(numbers);
System.debug('Adding element "10" at index 3');
numbers.add(3, 10);
System.debug('Updated List = ' + numbers);


Output:

As you can see above, initially the list was: 1, 2, 3, 4, 5. The elements and their indexes for the list are shown below:
Then, we're going to add a new element with value 10 at index 3, so, the values 4 and 5 will be shifted ahead to index 4 and 5 respectively and the updated list will be:

Removing element from the list

We can remove an element from the list by using the remove() method. The remove() method accepts the index of the element that you want to remove. For example: If in the above list you want to remove element 10, then you can pass it's index i.e. 3 in the remove method as follows:

Code Snippet:
List<Integer> numbers = new List<Integer>{1,2,3,10,4,5};
System.debug(numbers);
System.debug('Removing element "10" from index 3');
numbers.remove(3);
System.debug('Updated List = ' + numbers);

Output:

These were the basic methods using which you can interact with the list data structure. There are some other methods as well. I am going to discuss those quickly below with a one line description and code snippets.

Other Commonly used List methods

clear() - Clear all elements in the list

Code Snippet:
List<Integer> numbers = new List<Integer>{1,2,3,4,5};
System.debug(numbers);
System.debug('Clearing list...');
numbers.clear();
System.debug('Updated list = ' + numbers);

Output:
As you can see above, we created a list of numbers, we used clear() method to clear the list. Then we displayed the list got a confirmation that it's empty.

isEmpty() - Returns true if the list is empty, otherwise returns false

Code Snippet:
List<Integer> numbers = new List<Integer>();
System.debug('List Empty --> ' + numbers.isEmpty());
System.debug('Adding "1" to the list');
numbers.add(1);
System.debug('List Empty --> ' + numbers.isEmpty());

Output:
As you can see above, initially, the list was empty, so the isEmpty() method is returning true when called on the numbers list. After that, we added "1" to the list and then the isEmpty() method is returning false.

size() - Returns the size of the list i.e. the total number of elements present in the list

Code Snippet:
List<Integer> numbers = new List<Integer>{1,2,3,4,5};
System.debug(numbers);
System.debug('Size of list = ' + numbers.size());

Output:
As you can see above, I have 5 elements in my list of numbers, so, the size of list is coming out to be 5.

contains() - Returns true if a particular element is present in the list, otherwise, returns false

Code Snippet:
List<Integer> numbers = new List<Integer>{1,2,3,4,5};
System.debug(numbers);
System.debug('3 is present in the list --> ' + numbers.contains(3));
System.debug('Removing 3 from index 2');
numbers.remove(2);
System.debug(numbers);
System.debug('3 is present in the list --> ' + numbers.contains(3));

Output:
As you can see above, we had 5 elements in the list initially: 1, 2, 3, 4, 5. We checked for element 3 using contains() and it returned true as 3 was present in the list. The element 3 was present at index 2 so, we removed 3 from the list by using remove() method and the updated list is: 1, 2, 4, 5. Finally, we checked for element 3 again if it's present in the list using contains() and this time, the result comes out to be false.

sort() - Sort the elements in the list in ascending order

Code Snippet:
List<Integer> numbers = new List<Integer>{1,4,5,3,2};
System.debug(numbers);
System.debug('Sorting list...');
numbers.sort();
System.debug(numbers);

Output:
As you can see above, initially, the list had elements: 1, 4, 5, 3, 2. Then we sorted the list using sort() and now, the list consist of elements in sorted order i.e.: 1, 2, 3, 4, 5.

These are the most common methods that are used while interacting with a list, although there are some other methods as well, I would suggest to have a look at the official salesforce documentation for more information.

That's all for this tutorial everyone, I hope you liked it. Let me know your feedback or if you have any queries in the comments down below. You can find the whole code snippet used in this tutorial here.


Happy Trailblazing..!!

No comments:

Post a Comment