· development · 3 min read
Salesforce Apex List Class Guide
The Apex List class provides various methods to work with lists like adding elements, removing elements, accessing elements by index, sorting, searching.
Introduction
A list is a collection that allows you to store multiple values of the same data type in a specific order. The Salesforce Apex List class provides methods to work with lists efficiently.
Key features and characteristics of Salesforce Apex lists include:
- Homogeneous Elements: A list in Apex can store elements of the same data type. For example, a list can hold a collection of strings, integers, custom objects, or any other data type.
- Dynamic Size: Lists in Apex are dynamic, meaning they can grow or shrink as elements are added or removed. You don’t need to specify the initial size of the list.
- Ordered Collection: Elements in a list maintain their order based on the index position. The first element has an index of 0, the second element has an index of 1, and so on.
- Zero-Based Indexing: Apex list elements are accessed using a zero-based index. This means that the first element is at index 0, the second element is at index 1, and so on.
- Methods and Operations: The Apex List class provides various methods and operations to manipulate and work with lists. These include adding elements, removing elements, accessing elements by index, sorting, searching, and more.
- Iteration: Lists can be iterated using loops or iterators, allowing you to process each element sequentially.
Salesforce Apex lists are commonly used to store and manage collections of data, such as records retrieved from a database, query results, or any set of related values. They offer flexibility, ease of use, and powerful operations for working with collections of data in the Salesforce platform.
Apex list methods
Let’s take a look at the methods the Apex List Class provides.
add
The List Class provides two add methods to add elements to a list.
To add an element by appending it to the list at the end use the following variant of the add list.
Void add(Object listElement))
- listElement (Object): The object to add to this list.
- Void: The list add method returns nothing.
List<String> fruits = new List<String>();
fruits.add('apple');
System.assertEquals('apple', fruits.get(0));
To add an element at a postion of your choice, you need to provide the position and the element to the add method.
Void add(Integer index, Object listElement))
- index (Integer): The position where to add the element within this list.
- listElement (Object): The object to add to this list.
- Void: The list add method returns nothing.
List<String> fruits = new String[4];
fruits.add(3,'apple');
System.assertEquals('apple', fruits.get(3));
addAll
To merge to lists, you can call the addAll method an list and pass a another list to it. This way the list gets the added elements appended at the end.
Void addAll(List fromList))
- Void addAll(List fromList) (List): The list with the elements to add to this list.
- Void: The list addAll method returns nothing.
List<String> fruits = new List<String>();
fruits.add('apple');
List<String> moreFruits = new List<String>();
moreFruits.add('orange');
fruits.addAll(moreFruits);
System.assertEquals('orange', fruits.get(1));
To add a set, you can also call the addAll method.
Void addAll(Set fromSet))
- fromSet (Set): The set with the elements to add to this list.
- Void: The list addAll method returns nothing.
List<String> fruits = new List<String>();
fruits.add('apple');
Set<String> moreFruits = new Set<String>();
moreFruits.add('orange');
fruits.addAll(moreFruits);
System.assertEquals('orange', fruits.get(1));
clear
To remove all elements of a list call the clear method.
Void clear())
- Void: The list clear method returns nothing.
List<String> fruits = new List<String>();
fruits.add('apple');
System.assertEquals(1, fruits.size());
fruits.clear();
System.assertEquals(0, fruits.size());
contains
In case you need a to know, whether an element is within a list or not, use the contains method.
Boolean contains(Object listElement))
- listElement (Object): The object to check if it is in the list or not.
- Boolean: True if the list includes the specified element, false if not.
List<String> fruits = new List<String>();
System.assertEquals(false, fruits.contains('apple'));
fruits.add('apple');
System.assertEquals(true, fruits.contains('apple'));
get
To get an element of a list at concrete position, use the get method and pass the position to the method.
Object get(Integer index))
- index (Integer): The position for the element to be retrieved.
- Object: The object at the given position within the list.
List<String> fruits = new List<String>();
try {
System.assertEquals(null, fruits.get(0));
System.Assert.fail();
} catch (System.ListException e) {
}
fruits.add('apple');
System.assertEquals('apple', fruits.get(0));
indexOf
To find out, at which index an element is within a list, use the indexOf method.
Integer indexOf(Object listElement))
- listElement (Object): The object for which the index needs to be found.
- Integer: The index of the given element in this list or -1 in case the element is not in this list.
List<String> fruits = new List<String>();
fruits.add('apple');
fruits.add('orange');
System.assertEquals(1, fruits.indexOf('orange'));
System.assertEquals(-1, fruits.indexOf('melon'));
isEmpty
To check if a list is empty or not, use the isEmpty method.
Boolean isEmpty())
- Boolean: True, if this list has no elements. False, if this list has elements.
List<String> fruits = new List<String>();
System.assertEquals(true, fruits.isEmpty());
fruits.add('apple');
System.assertEquals(false, fruits.isEmpty());
iterator
To iterate over a list, you can use the iterator you retrieve by the iterator method.
Iterator iterator())
- Iterator: An iterator instance for this list.
List<String> fruits = new List<String>();
fruits.add('apple');
Iterator<String> it = fruits.iterator();
while(it.hasNext()){
System.debug('iterate');
System.assertEquals('apple',it.next());
}
remove
To remove an element of a list, use the remove method.
Object remove(Integer index))
- index (Integer): Position of the element to be removed from this list.
- Object: The removed object.
List<String> fruits = new List<String>();
fruits.add('apple');
System.assertEquals('apple',fruits.remove(0));
try {
fruits.remove(1);
System.Assert.fail();
}catch (System.ListException e){
}
set
To set the element of list at a concrete position, use the set method.
Void set(Integer index, Object listElement))
- index (Integer): The position of the element to be set in this list.
- listElement (Object): The element that will be set at the given index.
- Void: The list set method returns nothing.
List<String> fruits = new List<String>();
System.assertEquals(0,fruits.size());
fruits.add('apple');
fruits.set(0,'orange');
System.assertEquals('orange', fruits.get(0));
size
To find out the size of a list, use the size method.
Integer size())
- Integer: The number of elements in this list.
List<String> fruits = new List<String>();
System.assertEquals(0,fruits.size());
fruits.add('apple');
System.assertEquals(1, fruits.size());
sort
To sort the elements of a list, use the sort method.
Void sort())
- Void: The list sort method returns nothing.
List<String> fruits = new List<String>();
fruits.add('orange');
fruits.add('apple');
fruits.add('melon');
System.assertEquals('orange',fruits.get(0));
System.assertEquals('apple',fruits.get(1));
System.assertEquals('melon',fruits.get(2));
fruits.sort();
System.assertEquals('apple',fruits.get(0));
System.assertEquals('melon',fruits.get(1));
System.assertEquals('orange',fruits.get(2));