The topic of lists in Apex is highly bound to other concepts such as loops, SOQL, DML and triggers. We’ll take some time to understand lists now, but you’ll see their true power when we discuss those other topics.
Lists, sets and maps are all collections. Let’s discuss lists and sets and leave out maps for the time being.

Create a list in Apex
A list is a collection of objects of one type. Let’s start by creating a list of names:
List<String> names = new List<String>();
We can put as many String values into this list as we want. Of course, we can only put “strings” inside a string list. However, the list can have any other data type:
List<Integer> integers = new List<Integer>();
List<Lead> leads = new List<Lead>();
List<Person> people = new List<Person>();
List<Student__c> students = new List<Student__c>();
The syntax of lists in Apex is as follows:
List<DataType> listName = new List<DataType>();
Here:
- DataType: Any supported data type in Apex. This data type specifies the type of elements we can put into the list.
- List: A special data type called “List” with the special notation to specify the type of the list elements.
- listName: The variable name.
- new List(): Initialization of an empty list.
Is list in Apex a data type?
Yes, it is! As discussed in the “Variables in Apex” article, we’ve declared a variable with the List type and then initialized the variable with the new List(); value.
Therefore, list is just a data type like Contact or Person! It has all the attributes of a data type: You can create new variables of this type, you use the new keyword and the class has methods.
The best thing about it is that we already know what we can do with a variable: call methods on it, pass into the method, output, etc. We can do all that with the list variable as well.
Adding elements to a List in Apex
You can add elements to the list by using the add method from the list class. This method accepts any object if the type of the object matches the type of the list:
List<DataType> listName = new List<DataType>();
listName.add(dataTypeObject);
Here:
- dataTypeObject: An object of the same data type as the list data type.
Example #1 – Names
We can add names to the list:
List<String> names = new List<String>();
names.add('Alex');
names.add('John');
names.add('Oliver');
Now the list contains three names that we can output to the console.
List<String> names = new List<String>();
names.add('Alex');
names.add('John');
names.add('Oliver');
System.debug(names);
Output:
(Alex, John, Oliver)
Notice that the output is enclosed in parentheses “(…)”. This is just a way to display a list in the logs.
Example #2 – Integers
List<Integer> ages = new List<Integer>();
names.add(23);
names.add(71);
names.add(38);
System.debug(ages);
Output:
(23, 71, 38)
Example #3 – Contacts
In the real world, we usually use lists with some custom or standard objects:
List<Contact> contacts = new List<Contact>(); // Initialize list
Contact myContact1 = new Contact(); // Create first contact
myContact1.LastName = 'Alex Johnson'; // Initialize LastName field on the object
Contact myContact2 = new Contact(); // Create second first contact
myContact2.LastName = 'John Miller'; // Initialize LastName field on the object
contacts.add(myContact1); // Add first contact to the list
contacts.add(myContact2); // Add second contact to the list
System.debug(contacts); // Print the whole list
Output:
(Contact:{LastName=Alex Johnson}, Contact:{LastName=John Miller})
Getting elements from a list
Obviously, if you can put an element into a list, you can get an element from a list as well. Each element in the array has an index that you can use to access the element behind this index. Let’s go back to our example with names:
List<String> names = new List<String>();
names.add('Alex'); // Index = 0
names.add('John'); // Index = 1
names.add('Oliver'); // Index = 2
Index is nothing more than an element’s place in the list (but starting with 0). The first element in programming typically starts with 0 because your hardware (CPU) works that way. There are some programming languages, though, that start with 1 instead of 0.

This is how you can get an element from the list:
List<String> names = new List<String>();
names.add('Alex'); // Index = 0
names.add('John'); // Index = 1
names.add('Oliver'); // Index = 2
System.debug(names.get(1)); // John

You can go up to the maximal index in the list and get any element:
List<String> names = new List<String>();
names.add('Alex'); // Index = 0
names.add('John'); // Index = 1
names.add('Oliver'); // Index = 2
System.debug(names.get(0)); // Alex
System.debug(names.get(1)); // John
System.debug(names.get(2)); // Oliver

If you try to call names.get(3);, you’ll get an error because there’s no element under such an index in the list.

The get method isn’t different from other methods and returns a value of a certain type. You can assign this value to a variable and work with it.
List<String> names = new List<String>();
names.add('Alex'); // Index = 0
names.add('John'); // Index = 1
names.add('Oliver'); // Index = 2
String secondElement = names.get(0);
secondElement = secondElement + ' Miller';
System.debug(secondElement); // output changed second element
System.debug(names); // output list
Output:
John Miller
(Alex, John, Oliver)
As you can see, the list remains unchanged. The reason is that when you call the get method and assign the value from this call to a new variable, Apex creates a copy of the value from the list.
List size
A list will have as many elements as you put inside it. You can check how many elements are inside a list by calling the size method from the List class:
List<String> names = new List<String>();
names.add('Alex');
names.add('John');
names.add('Oliver');
System.debug(names.size()); // 3
Adding elements during list initialization
If you know which values should be in your list, you can create a list directly with these values:
List<String> names = new List<String>{'Alex', 'John', 'Oliver'};
Or you can use variables as well:
String alexName = 'Alex';
String johnName = 'John';
String oliverName = 'Oliver';
List<String> names = new List<String> {alexName, johnName, oliverName};
There’s no difference at all between adding the values into the list immediately or using the add method. Usually, code with the add method looks much cleaner.
List methods
There are many methods that we can use on lists, but most of them are almost never used. You can check them all out in the official docs. Let’s walk through the methods that are actually helpful:
- addAll(anotherList): This method is similar to .add(element), but instead of adding an element, it adds all the elements from another list. Here’s an example:
List<String> firstList = new List<String>();
firstList.add('Alex');
firstList.add('John');
List<String> secondList = new List<String>();
secondList.add('Oliver');
secondList.addAll(firstList); // adds all elements from the first list to the second
System.debug(secondList); // (Oliver, Alex, John)
- isEmpty(): Returns true if the list has zero elements and false otherwise. It’s basically just a short version of the names.size() == 0 check.
- remove(index): Removes an element by index. This is helpful if you know the index of the element that you want to remove, but it’s not used very often in the real world.
List<String> names = new List<String>();
names.add('Alex');
names.add('John');
names.add('Oliver');
names.remove(0);
System.debug(names); // (John, Oliver)
- contains(listElement): Returns true if the list contains the element and false otherwise.
List<String> names = new List<String>();
names.add('Alex');
names.add('John');
names.add('Oliver');
System.debug(names.contains('John')); // true
System.debug(names.contains('Oliver')); // true
System.debug(names.contains('Test')); // false
System.debug(names.contains('Igor')); // false
Sets in Apex
Sets are just lists, but you can only put unique elements inside them. The syntax is identical to that of lists:
Set<DataType> setName = new Set<DataType>();
Let’s see what the difference is with Lists. This code would work in Apex:
List<String> names = new List<String>();
names.add('Alex');
names.add('Alex');
names.add('Alex');
System.debug(names); // (Alex, Alex, Alex)
If you try to run the same code with Sets, the code will work, but the output will be different:
Set<String> names = new Set<String>();
names.add('Alex');
names.add('Alex');
names.add('Alex');
System.debug(names); // {Alex}
The reason is that every time you call the add(element) method in the set, Apex checks if the value is already in the set. If it’s in the set, it won’t add the value to the set. Sets are mostly used to work with unique data (e.g. ids of records or country names). We’ll see this when we discuss DMLs.
Is that all we should know about lists and sets?
No! When I was writing this article, I had to omit a lot of things because we haven’t gone over loops, SOQL and DML yet. You’ll learn a lot more about lists in the next few chapters when we discuss these topics.
Links
- List Data Structure in Apex (SFDC Stop).
- List methods (Salesforce Documentations).
- Good article about lists (SFDCMeet).
- Why Programmers Count From Zero? (Betterprogrammer).
1 Comment