· 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.

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.

<List>.add
Void add(Object listElement))
Appends an element to the end of the list.
Input parameter
  • listElement (Object): The object to add to this list.
Return value
  • Void: The list add method returns nothing.
Example
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.

<List>.add
Void add(Integer index, Object listElement))
Places an element into the list at the specified index position.
Input parameter
  • index (Integer): The position where to add the element within this list.
  • listElement (Object): The object to add to this list.
Return value
  • Void: The list add method returns nothing.
Example
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.

<List>.addAll
Void addAll(List fromList))
Appends all elements from the specified list to the calling list. Both lists must be of the same type.
Input parameter
  • Void addAll(List fromList) (List): The list with the elements to add to this list.
Return value
  • Void: The list addAll method returns nothing.
Example
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.

<List>.addAll
Void addAll(Set fromSet))
Appends all elements from the specified set to the calling list. The set must be of the same type as the list.
Input parameter
  • fromSet (Set): The set with the elements to add to this list.
Return value
  • Void: The list addAll method returns nothing.
Example
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.

<List>.clear
Void clear())
Clears all elements from the list, effectively resetting its length to zero.
Return value
  • Void: The list clear method returns nothing.
Example
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.

<List>.contains
Boolean contains(Object listElement))
It returns true if the list includes the specified element.
Input parameter
  • listElement (Object): The object to check if it is in the list or not.
Return value
  • Boolean: True if the list includes the specified element, false if not.
Example
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.

<List>.get
Object get(Integer index))
Retrieves the list element stored at the specified index.
Input parameter
  • index (Integer): The position for the element to be retrieved.
Return value
  • Object: The object at the given position within the list.
Example
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.

<List>.indexOf
Integer indexOf(Object listElement))
It returns the index of the first occurrence of the specified element in this list. If the element is not found, it returns -1.
Input parameter
  • listElement (Object): The object for which the index needs to be found.
Return value
  • Integer: The index of the given element in this list or -1 in case the element is not in this list.
Example
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.

<List>.isEmpty
Boolean isEmpty())
It returns true if the list contains no elements.
Return value
  • Boolean: True, if this list has no elements. False, if this list has elements.
Example
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.

<List>.iterator
Iterator iterator())
Provides an iterator instance for this list.
Return value
  • Iterator: An iterator instance for this list.
Example
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.

<List>.remove
Object remove(Integer index))
Deletes the list element at the specified index and returns the removed element.
Input parameter
  • index (Integer): Position of the element to be removed from this list.
Return value
  • Object: The removed object.
Example
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.

<List>.set
Void set(Integer index, Object listElement))
Assigns the specified value to the element at the given index.
Input parameter
  • 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.
Return value
  • Void: The list set method returns nothing.
Example
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.

<List>.size
Integer size())
Provides the count of elements in the list.
Return value
  • Integer: The number of elements in this list.
Example
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.

<List>.sort
Void sort())
Arranges the items in the list in ascending order.
Return value
  • Void: The list sort method returns nothing.
Example
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));
Share:
Back to Blog