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.
Tutorial Video
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
List<Integer> numbers = new List<Integer>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); System.debug(numbers);
Accessing elements from the list
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));
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: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);
Other Commonly used List methods
clear() - Clear all elements in the list
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);
isEmpty() - Returns true if the list is empty, otherwise returns false
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());
size() - Returns the size of the list i.e. the total number of elements present in the list
List<Integer> numbers = new List<Integer>{1,2,3,4,5}; System.debug(numbers); System.debug('Size of list = ' + numbers.size());
contains() - Returns true if a particular element is present in the list, otherwise, returns false
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));
sort() - Sort the elements in the list in ascending order
List<Integer> numbers = new List<Integer>{1,4,5,3,2}; System.debug(numbers); System.debug('Sorting list...'); numbers.sort(); System.debug(numbers);
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..!!
Good explanation
ReplyDeleteAmazing
ReplyDelete