Lists in APEX
Chapter Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types Guide
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
A list in Apex is an ordered collection of elements used to store data in sequence. These elements can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Let’s understand Lists in Apex with examples
Suppose there is a list (‘Violet’, ‘Indigo’, ‘Blue’, ‘Yellow’, ‘Red’, ‘Purple’).
The index and value of each element will be as follows:
| Violet | Indigo | Blue | Yellow | Red | Purple |
|---|---|---|---|---|---|
| 0th | 1st | 2nd | 3rd | 4th | 5th |
Why do We Use Lists in Salesforce?
Lists in Salesforce, specifically in Apex, are used for several important reasons:
- Dynamic Size: Lists can grow or shrink, allowing you to store an unknown number of elements without predefining their size.
- Data Management: They help in managing collections of data, such as SObject records or custom data types, efficiently. This is particularly useful when handling bulk data operations.
- Iteration and Access: Lists allow easy iteration and element access via indexes. This makes it simple to loop through collections of records or other data structures.
- Flexibility: You can add, remove, and manipulate elements within lists easily. This flexibility is beneficial when developing complex business logic and algorithms.
- Integration with Salesforce Features: Lists integrate seamlessly with many Salesforce features. For instance, when querying records with SOQL (Salesforce Object Query Language), the results are often stored in lists.
- Support for Complex Data Structures: Lists can be used to create complex data structures, such as lists of lists, maps of lists, and so on, providing powerful ways to structure and process data.
Properties Of Lists In Apex
There are 2 main properties of lists:
- It stores data in sequential order.
- The data that it stores is non-unique or can be duplicated.
Hence, use a list when the sequence of elements is important and when the uniqueness of the elements is not.
How to Create a List of Lists in Apex?
Creating a list of lists in Salesforce Apex is straightforward. Here’s a step-by-step guide to help you understand the process:
- Define a List: Start by defining a list. In Apex, lists are dynamic arrays that can hold collections of objects.
- Create a List of Lists: You can create a list of lists by defining a list whose elements are also lists.
For example:
// Create individual lists
List<String> firstList = new List<String>{'A', 'B', 'C'};
List<String> secondList = new List<String>{'D', 'E', 'F'};
List<String> thirdList = new List<String>{'G', 'H', 'I'};
// Create a list of lists
List<List<String>> listOfLists = new List<List<String>>();
// Add individual lists to the list of lists
listOfLists.add(firstList);
listOfLists.add(secondList);
listOfLists.add(thirdList);
// Accessing elements from the list of lists
System.debug(listOfLists[0][1]); // Output: B
System.debug(listOfLists[1][2]); // Output: F
System.debug(listOfLists[2][0]); // Output: G
List Methods in Apex
The following are methods for List. All are instance methods.
| S.No | Function | Example |
|---|---|---|
| 1 | add(listElement) Adds an element to the end of the list. |
List |
| 2 | add(index, listElement) Inserts an element into the list at the specified index position. |
List |
| 3 | addAll(fromList) Adds all of the elements in the specified list to the list that calls the method. Both lists must be of the same type. |
List |
| 4 | addAll(fromSet) Adds all of the elements in the specified set to the list that calls the method. The set and the list must be of the same type. |
List |
| 5 | clear() removes all elements from a list, setting its length to 0. |
List |
| 6 | clone() Makes a duplicate copy of a list. |
List |
| 7 | contains(listElement) Returns true if the list contains the specified element. |
List |
| 8 | deepClone(preserveId, preserveReadonlyTimestamps, preserveAutonumber) Makes a duplicate copy of a list of sObject records, including the sObject records themselves. |
Account a = new Account(Name='Acme', BillingCity='New York'); |
| 9 | equals(list2) Compares this list with the specified list and returns true if both lists are equal; otherwise, returns false. |
List |
| 10 | get(index) Returns the list element stored at the specified index. |
List |
| 11 | getSObjectType() Returns the token of the sObject type that makes up a list of sObjects. |
SObject sObj = [SELECT Id FROM Account LIMIT 1]; |
| 12 | hashCode() Returns the hashcode corresponding to this list and its contents. |
List |
| 13 | indexOf(listElement) Returns the index of the first occurrence of the specified element in this list. If this list does not contain the element, returns -1. |
List |
| 14 | isEmpty() Returns true if the list has zero elements. |
List |
| 15 | iterator() Returns an instance of an iterator for this list. |
List |
| 16 | remove(index) Removes the list element stored at the specified index, returning the element that was removed. |
List |
| 17 | set(index, listElement) Sets the specified value for the element at the given index. |
List |
| 18 | size() Returns the number of elements in the list. |
List |
| 19 | sort() Sorts the items in the list in ascending order. |
List |
| 20 | toString() Returns the string representation of the list. |
List |

Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.