Newer Version Available
List Class
Namespace
Usage
The list methods are all instance methods, that is, they operate on a particular instance of a list. For example, the following removes all elements from myList:
Even though the clear method does not include any parameters, the list that calls it is its implicit parameter.
For more information on lists, see Lists.
List Constructors
The following are constructors for List.
List<T>()
Signature
public List<T>()
Example
List<T>(listToCopy)
Signature
public List<T>(List<T> listToCopy)
Parameters
- listToCopy
- Type: List<T>
- The list containing the elements to initialize this list from. T is the data type of the list elements.
Example
List<T>(setToCopy)
Signature
public List<T>(Set<T> setToCopy)
Parameters
- setToCopy
- Type: Set<T>
- The set containing the elements to initialize this list with. T is the data type of the set elements.
Example
List Methods
The following are methods for List. All are instance methods.
add(listElement)
Signature
public Void add(Object listElement)
Parameters
- listElement
- Type: Object
Return Value
Type: Void
Example
add(index, listElement)
Signature
public Void add(Integer index, Object listElement)
Parameters
- index
- Type: Integer
- listElement
- Type: Object
Return Value
Type: Void
Example
In the following example, a list with six elements is created, and integers are added to the first and second index positions.
addAll(fromList)
Signature
public Void addAll(List fromList)
Parameters
- fromList
- Type: List
Return Value
Type: Void
addAll(fromSet)
Signature
public Void addAll(Set fromSet)
Parameters
- fromSet
- Type: Set
Return Value
Type: Void
clear()
Signature
public Void clear()
Return Value
Type: Void
clone()
Signature
public List<Object> clone()
Return Value
Type: List<Object>
Usage
The cloned list is of the same type as the current list.
Note that if this is a list of sObject records, the duplicate list will only be a shallow copy of the list. That is, the duplicate will have references to each object, but the sObject records themselves will not be duplicated. For example:
To also copy the sObject records, you must use the deepClone method.
Example
contains(listElement)
Signature
public Boolean contains(Object listElement)
Parameters
- listElement
- Type: Object
Return Value
Type: Boolean
Example
deepClone(preserveId, preserveReadonlyTimestamps, preserveAutonumber)
Signature
public List<Object> deepClone(Boolean preserveId, Boolean preserveReadonlyTimestamps, Boolean preserveAutonumber)
Parameters
- preserveId
- Type: Boolean
- The optional preserveId argument determines whether the IDs of the original objects are preserved or cleared in the duplicates. If set to true, the IDs are copied to the cloned objects. The default is false, that is, the IDs are cleared.
- preserveReadonlyTimestamps
- Type: Boolean
- The optional preserveReadonlyTimestamps argument determines whether the read-only timestamp and user ID fields are preserved or cleared in the duplicates. If set to true, the read-only fields CreatedById, CreatedDate, LastModifiedById, and LastModifiedDate are copied to the cloned objects. The default is false, that is, the values are cleared.
- preserveAutonumber
- Type: Boolean
- The optional preserveAutonumber argument determines whether the autonumber fields of the original objects are preserved or cleared in the duplicates. If set to true, auto number fields are copied to the cloned objects. The default is false, that is, auto number fields are cleared.
Return Value
Type: List<Object>
Usage
The returned list is of the same type as the current list.
To make a shallow copy of a list without duplicating the sObject records it contains, use the clone method.
Example
This example performs a deep clone for a list with two accounts.
equals(list2)
Signature
public Boolean equals(List list2)
Parameters
- list2
- Type: List
- The list to compare this list with.
Return Value
Type: Boolean
Usage
Two lists are equal if their elements are equal and are in the same order. The == operator is used to compare the elements of the lists.
The == operator is equivalent to calling the equals method, so you can call list1.equals(list2); instead of list1 == list2;.
get(index)
Signature
public Object get(Integer index)
Parameters
- index
- Type: Integer
Return Value
Type: Object
Usage
To reference an element of a one-dimensional list of primitives or sObjects, you can also follow the name of the list with the element's index position in square brackets as shown in the example.
Example
getSObjectType()
Signature
public Schema.SObjectType getSObjectType()
Return Value
Type: Schema.SObjectType
Usage
Use this method with describe information to determine if a list contains sObjects of a particular type.
Note that this method can only be used with lists that are composed of sObjects.
For more information, see Understanding Apex Describe Information.
Example
hashCode()
Signature
public Integer hashCode()
Return Value
Type: Integer
indexOf(listElement)
Signature
public Integer indexOf(Object listElement)
Parameters
- listElement
- Type: Object
Return Value
Type: Integer
Example
isEmpty()
Signature
public Boolean isEmpty()
Return Value
Type: Boolean
iterator()
Signature
public Iterator iterator()
Return Value
Type: Iterator
Usage
From the returned iterator, you can use the iterable methods hasNext and next to iterate through the list.
See Custom Iterators.
Example
remove(index)
Signature
public Object remove(Integer index)
Parameters
- index
- Type: Integer
Return Value
Type: Object
Example
set(index, listElement)
Signature
public Void set(Integer index, Object listElement)
Parameters
- index
- Type: Integer
- The index of the list element to set.
- listElement
- Type: Object
- The value of the list element to set.
Return Value
Type: Void
Usage
To set an element of a one-dimensional list of primitives or sObjects, you can also follow the name of the list with the element's index position in square brackets.
Example
size()
Signature
public Integer size()
Return Value
Type: Integer
Example
sort()
Signature
public Void sort()
Return Value
Type: Void
Usage
Using this method, you can sort primitive types, SelectOption elements, and sObjects (standard objects and custom objects). For more information on the sort order used for sObjects, see Sorting Lists of sObjects. You can also sort custom types (your Apex classes) if they implement the Comparable Interface interface.
When you use sort() methods on List<Id>s that contain both 15-character and 18-character IDs, IDs for the same record sort together in API version 35.0 and later.
Example
In the following example, the list has three elements. When the list is sorted, the first element is null because it has no value assigned while the second element has the value of 5.
toString()
Signature
public String toString()
Return Value
Type: String
Usage
- Up to 10 items per collection are included in the output, followed by an ellipsis (…).
- If the same object is included multiple times in a collection, it’s shown in the output only once; subsequent references are shown as (already output).