While working as a Salesforce developer, I was tasked with building an Apex class to automate a customer support process.
The requirement was to retrieve all open cases for a specific customer to see if their status was “New” or “In Progress.”
To store multiple records in Salesforce Apex, we use a collection that stores multiple records or values. Here, I will explain how to create a list collection in Salesforce Apex programming and show some methods for manipulating the list collection.
List Collection in Salesforce Apex
The list is a collection data type in which information is stored in a sequential format with an index. The list in Apex is similar to the array in Java. Each list index begins with 0.
The list can store duplicate and null values. In Apex, the list size can be increased or decreased at runtime, depending on the number of elements added to it.
A List in Salesforce is an ordered collection of elements that can hold multiple records or values of any data type (such as primitives, objects, or sObjects).
It allows for duplicates and maintains the order of insertion. Lists are commonly used for bulk operations, such as querying multiple records or performing bulk DML (Data Manipulation Language) actions.
- When we execute the list, we get the output in the order in which we enter the elements.
- The values can be duplicated in the list; the list doesn’t restrict the duplicate values in the apex collection.
Syntax of declaring a list collection:
List <Datatype> myList = new <Datatype>();
List<Integer>myList = new List<Integer>{1,8,6,4,6,9}; //declare and add values to list.Methods for List in Salesforce Apex Programming
We have seen what a list collection is and how to declare it. Now, we will see methods used in list manipulation in Salesforce Apex.
The following are the methods that we can use in the list, and I will explain all methods with examples:
- Add(Element).
- Add(Index, Element).
- Remove(Index).
- Clone().
- Size().
- Equals().
1. Add(Element) Method in List Collection
The add() method in the list collection is used to add a new element to the list collection, and using this method, we can add individual values.
In this method, when we add an element, it is added at the last position, or we can say that the element is appended.
In the following apex code, I first declare a list variable with a list collection and then, using the add() method, add some values to the list collection. As you display the list, you will see the added elements.
List<Integer>myList = new List<Integer>();
myList.add (5);
myList.add (3);
myList.add (9);
myList.add (2);
myList.add (3);
system.debug( 'Collection of List is: ' +myList);As you execute the code, you can see that an integer has been added to the list, and it also displays duplicate values.

2. Add(Index, Element) Method in List Collection
This method is identical to the add() method above. However, this method has two parameters to enter: index and element. This index is the position of the component we want to add to the list.
For example, we already have one list of integers, and we want to add the ‘500’ value at the third position in the list.
In the following Apex program, using myList.add(2,500) method, and we added an element at the third position in the existing list. In the method, first, we enter a position.
The position starts from 0 indices, and we want to add an element to the third position. For that, we need to enter two as the index and the value you want to add.
List<Integer>myList = new List<Integer>{950,840,200,410,650,920};
system.debug( ' Before adding 500 value in the list Collection: ' +myList);
myList.add(2,500);
system.debug( ' After adding 500 value to 3rd Position: ' +myList);As we execute the Apex code, you can see the list displayed after adding an element in the 3rd position.

3. Remove(Index) Method in List Collection
The remove() method in the list collection deletes an element or value from a specific position in the list. This method manipulates the list collection by removing elements that are no longer needed.
Here, first, I declared a list that stored string values. Using the remove() method, I wanted to remove the second indexed value and then display the remaining values.
List TechList = new List{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
system.debug( ' Before removing values from list: ' +TechList);
TechList.remove(2);
system.debug( ' After removing values from lis: ' +TechList);As you execute the code, you can see that the ‘SharePoint’ string value has been removed because we provided two indexes in the remove() method.
When we count the index from 0, the SharePoint value reaches the 2nd position.

4. Clear() Method in List Collection
In the list, there is another method for removing elements called the clear() method. When we want to remove or delete all values from the list, we can use the clear() method in Salesforce Apex.
Here, we used the clear() method to remove all values from the list collection and display the modified list. As you execute this Apex code, you will see there will be no values in the list.
List<String> TechList = new List<String>{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
system.debug( ' Before removing values from list: ' +TechList);
TechList.clear();
system.debug( ' After using clear method values: ' +TechList);
5. Clone() Method in List Collection
The clone() method in list collections is used in the Salesforce Apex program to create a new list of a collection that contains all the values of the original collection. It creates a copy of a collection that can be modified independently of the original.
First, I already declared one list with some values, and now I want to use that list for different purposes. Therefore, instead of creating a new list from scratch, we can clone (TechList.clone()) the original list and create an identical list to the original.
List<String> TechList = new List<String>{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
system.debug( ' Before cloning list values are: ' +TechList);
TechList.clone();
system.debug( ' After cloning the original list values new list is: ' +TechList);As you execute the Apex code, you will see that after using the clone() method, a new list is created with the same values that are present in the original list.

6. Size() Method in List Collection
The Size() method in list collections is used in the Salesforce Apex program to check the number of elements present in the list collection. It is usually used to check the size of the collection before performing operations on the list.
In the Apex code below, I declared the list and displayed the number of elements or values present in it using the size() method.
List<String> TechList = new List<String>{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
Integer listSize = TechList.size();
system.debug( ' The size of the TechList is: ' +listSize);
7. Equals() Method in List Collection
Using the Equals() method, we can compare two objects or variables to determine whether they are equal. Basically, it is used to compare two different strings or integers.
In the Apex code below, I explained how to use the equal() method to check whether the values in a list, string, or integer are the same.
First, I declared two variables, and we want to check whether they are equal. Then, to display the result, I declared a Boolean variable, which displays a True value if the variables are equal and returns False if they are not equal.
//Checking two lists are equal or not.
List<String> TechList1 = new List<String>{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
List<String> TechList2 = new List<String>{'Salesforce', 'PowerBI', 'SharePoint', 'AWS'};
Boolean listEqual = TechList1.equals(TechList2);
system.debug( ' Is these lists are equals: ' +listEqual);
//Checking two Strings are equal or not.
String str1 = 'Apex';
String str2 = 'Apex';
Boolean StrEqual = str1.equals(str2);
system.debug( ' Is these Strings are equals: ' +StrEqual);
//Checking two Integers are equal or not.
Integer no1 = 120;
Integer no2 = 500;
Boolean IntEqual = no1.equals(no2);
system.debug( ' Is these Numbers are equals: ' +IntEqual);As I execute the code here, you can see the result for the data types we used. The equal() method returns the Boolean value for the result.

These are the methods that we commonly use in list collection manipulation purposes using Salesforce Apex.
How to Use List in Salesforce Apex Code
For example, I want to store records of lead objects whose lead Industry is ‘Technology,’ and we need to update the Status of all those records to ‘Close-Converted.‘
Here, you can see that before creating the Apex code, the technology industry had a different status.

Here, I declared a class and created a list of lead records that store all leads with a technology industry focus. Now, we want to update all lead statuses to closed converted, which are from the technology industry. We need to add a loop so that we can assign a status to all lead records and update the list.
public class ListCollection {
public void UpdateLeadRecords(){
List<Lead> LeadsList = [Select Id, Name from Lead Where Industry='Technology'];
try{
for(Lead l : LeadsList){
l.Status = 'Close-Converted';
}
Update LeadsList;
}
Catch(Exception e){
e.setMessage('Something Went Wrong');
}
}
}After the Apex code is executed, you can see in the lead object all the records with the technology industry’s status are close-converted.

In this way, we can use the list collection in Salesforce Apex.
Conclusion
I hope you have got an idea about list collection in Salesforce Apex. In this tutorial, I explained how to create a list collection in Salesforce Apex programming, and then I explained the syntax of some methods for manipulating the list collection. After that, we saw an example of list collection in Salesforce Apex, and I explained the code to update multiple records using list collection.
You may like to read:
- Variables and Data Types in Salesforce Apex
- Create SObject in Salesforce Apex
- Create Set Collection in Salesforce Apex
- Create Set Collection in Salesforce Apex
- Apex Trigger Handler and Helper Class in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.