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:
myList.clear();
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.
The following are constructors for List.
public List<T>()
// Create a list List<Integer> ls1 = new List<Integer>(); // Add two integers to the list ls1.add(1); ls1.add(2);
public List<T>(List<T> listToCopy)
List<Integer> ls1 = new List<Integer>(); ls1.add(1); ls1.add(2); // Create a list based on an existing one List<Integer> ls2 = new List<Integer>(ls1); // ls2 elements are copied from ls1 System.debug(ls2);// DEBUG|(1, 2)
public List<T>(Set<T> setToCopy)
Set<Integer> s1 = new Set<Integer>(); s1.add(1); s1.add(2); // Create a list based on a set List<Integer> ls = new List<Integer>(s1); // ls elements are copied from s1 System.debug(ls);// DEBUG|(1, 2)
The following are methods for List. All are instance methods.
public Void add(Object listElement)
Type: Void
List<Integer> myList = new List<Integer>(); myList.add(47); Integer myNumber = myList.get(0); system.assertEquals(47, myNumber);
public Void add(Integer index, Object listElement)
Type: Void
In the following example, a list with six elements is created, and integers are added to the first and second index positions.
List<Integer> myList = new Integer[6]; myList.add(0, 47); myList.add(1, 52); system.assertEquals(52, myList.get(1));
public Void addAll(List fromList)
Type: Void
public Void addAll(Set fromSet)
Type: Void
public Void clear()
Type: Void
public List<Object> clone()
Type: List<Object>
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.
Account a = new Account(Name='Acme', BillingCity='New York'); Account b = new Account(); Account[] q1 = new Account[]{a,b}; Account[] q2 = q1.clone(); q1[0].BillingCity = 'San Francisco'; System.assertEquals( 'San Francisco', q1[0].BillingCity); System.assertEquals( 'San Francisco', q2[0].BillingCity);
public Boolean contains(Object listElement)
Type: Boolean
List<String> myStrings = new List<String>{'a', 'b'}; Boolean result = myStrings.contains('z'); System.assertEquals(false, result);
public List<Object> deepClone(Boolean preserveId, Boolean preserveReadonlyTimestamps, Boolean preserveAutonumber)
Type: List<Object>
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.
This example performs a deep clone for a list with two accounts.
Account a = new Account(Name='Acme', BillingCity='New York'); Account b = new Account(Name='Salesforce'); Account[] q1 = new Account[]{a,b}; Account[] q2 = q1.deepClone(); q1[0].BillingCity = 'San Francisco'; System.assertEquals( 'San Francisco', q1[0].BillingCity); System.assertEquals( 'New York', q2[0].BillingCity);
insert q1; List<Account> accts = [SELECT CreatedById, CreatedDate, LastModifiedById, LastModifiedDate, BillingCity FROM Account WHERE Name='Acme' OR Name='Salesforce']; // Clone list while preserving timestamp and user ID fields. Account[] q3 = accts.deepClone(false,true,false); // Verify timestamp fields are preserved for the first list element. System.assertEquals( accts[0].CreatedById, q3[0].CreatedById); System.assertEquals( accts[0].CreatedDate, q3[0].CreatedDate); System.assertEquals( accts[0].LastModifiedById, q3[0].LastModifiedById); System.assertEquals( accts[0].LastModifiedDate, q3[0].LastModifiedDate);
public Boolean equals(List list2)
Type: Boolean
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;.
public Object get(Integer index)
Type: Object
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.
List<Integer> myList = new List<Integer>(); myList.add(47); Integer myNumber = myList.get(0); system.assertEquals(47, myNumber);
List<String> colors = new String[3]; colors[0] = 'Red'; colors[1] = 'Blue'; colors[2] = 'Green';
public Schema.SObjectType getSObjectType()
Type: Schema.SObjectType
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.
// Create a generic sObject variable. SObject sObj = Database.query('SELECT Id FROM Account LIMIT 1'); // Verify if that sObject variable is an Account token. System.assertEquals( Account.sObjectType, sObj.getSObjectType()); // Create a list of generic sObjects. List<sObject> q = new Account[]{}; // Verify if the list of sObjects // contains Account tokens. System.assertEquals( Account.sObjectType, q.getSObjectType());
public Integer hashCode()
Type: Integer
public Integer indexOf(Object listElement)
Type: Integer
List<String> myStrings = new List<String>{'a', 'b', 'a'}; Integer result = myStrings.indexOf('a'); System.assertEquals(0, result);
public Boolean isEmpty()
Type: Boolean
public Iterator iterator()
Type: Iterator
From the returned iterator, you can use the iterable methods hasNext and next to iterate through the list.
You do not have to implement the iterable interface to use the iterable methods with a list.
See Custom Iterators.
global class CustomIterable implements Iterator<Account>{ List<Account> accs {get; set;} Integer i {get; set;} public CustomIterable(){ accs = [SELECT Id, Name, NumberOfEmployees FROM Account WHERE Name = 'false']; i = 0; } global boolean hasNext(){ if(i >= accs.size()) { return false; } else { return true; } } global Account next(){ // 8 is an arbitrary // constant in this example // that represents the // maximum size of the list. if(i == 8){return null;} i++; return accs[i-1]; } }
public Object remove(Integer index)
Type: Object
List<String> colors = new String[3]; colors[0] = 'Red'; colors[1] = 'Blue'; colors[2] = 'Green'; String s1 = colors.remove(2); system.assertEquals('Green', s1);
public Void set(Integer index, Object listElement)
Type: Void
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.
List<Integer> myList = new Integer[6]; myList.set(0, 47); myList.set(1, 52); system.assertEquals(52, myList.get(1));
List<String> colors = new String[3]; colors[0] = 'Red'; colors[1] = 'Blue'; colors[2] = 'Green';
public Integer size()
Type: Integer
List<Integer> myList = new List<Integer>(); Integer size = myList.size(); system.assertEquals(0, size); List<Integer> myList2 = new Integer[6]; Integer size2 = myList2.size(); system.assertEquals(6, size2);
public Void sort()
Type: Void
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.
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.
List<Integer> q1 = new Integer[3]; // Assign values to the first two elements. q1[0] = 10; q1[1] = 5; q1.sort(); // First element is null, second is 5. system.assertEquals(5, q1.get(1));