Apex Collections:
The Apex language provides developers with three classes (Set, List and Map) that make it easier to handle collections of objects. In a sense these collections work somewhat like arrays, except their size can change dynamically, and they have more advanced behaviors and easier access methods than arrays.
The Apex language provides developers with three classes (Set, List and Map) that make it easier to handle collections of objects. In a sense these collections work somewhat like arrays, except their size can change dynamically, and they have more advanced behaviors and easier access methods than arrays.
Salesforce.com recently made an important change to collections. There is no longer a limit on the number of items a collection can hold.LIST:
A list is an Ordered collection of typed primitives, sObjects, user-defined objects, Apex objects or collections that are distinguished by their indices.
For Example: the following table is a visual representation of a list of Strings:
The index position of the first element in a list is always Starts with 0 (Zero).
Because lists can contain any collection, they can be nested within one another and become multidimensional.
Note: A list can only contain up to five levels of nested collections inside it.
List Syntax:
To declare a list, use the List keyword followed by the primitive data, sObject, nested list, map, or set type within < > characters.
List<datatype> listname;
Creating an empty list of String:
// Create an empty list of String
List<String> my_list = new List<String>();
// Create a nested list
List<List<Set<Integer>>> my_list_2 = new List<List<Set<Integer>>>();
// Create a list of account records from a SOQL query
List<Account> accs = [SELECT Id, Name FROM Account LIMIT 1000];
List Methods:The list methods are all instance methods, that is, they operate on a particular instance of a list. Now we will see the all the list methods.
ADD Method: Adds elements to a list.
// Add single integer element to list List<Integer> numlist = new List<Integer>(); numlist.add(23); // Add multipule elements to list List<Integer> numlist = new List<Integer>(30,20,1); // Add Strings to list List<String> namelist = new List<String>(); namelist.add('Nagarjun'); namelist.add('Malli'); namelist.add('Sreenu'); // Add s-Object to list List<Account> acclist = new List<Account>(); Account acc = New Account(); Acc.name = 'Nagarjun'; acclist.add(acc);
You Might Also Like: What Is a SET In Apex?
SET Method: Adds elements to a list.
// Set the inter to list List<Integer> numlist = new List<Integer>(); numlist.set(0,10); // Add's 10 at the first position. numlist.set(1,20); // Add's 20 at the second position. //This can also be achieved with the following:- Numlist[0] = 10; Numlist[1] = 20; // Set the Strings to list List<String> namelist = new List<String>(); namelist.set(0,'Nagarjun'); namelist.set(1,'Hari'); namelist.set(2,'Krishna');
GET Method: Retrieves element from a list.
// Get the Elements from List List<Integer> numlist = new List<Integer>(); numlist.add(23); Integer Mynum = numlist.get(0) //Retrieves first occurrence from the numlist. // Get the Strings from List List<String> namelist = new List<String>(); namelist.add('Nagarjun'); //Added Mark at first occurrence. namelist.add('Hari'); //Added Frank at Second occurrence. namelist.add('Krish'); String Myname = namelist.get(1) // Retrieves second occurrence i.e 'Hari' from the namelist.
Remove Method: Remove element from a list.
// Remove Integer from list List<Integer> numlist = new List<Integer>(20,10,30); numlist.remove(0); //Removes first occurrence i.e 20 from the numlist. // Remove Strings from List List<String> namelist = new List<String>('Nagarjun','Malli','Hari'); Namelist.remove(2) //Removes third occurrence i.e 'Hari' from the namelist.
Clear Method: Removes all the elements from the list and sets the size of the list to Zero.
// Remove all Integer elements from list List<Integer> numlist = new List<Integer>(20,10,30); numlist.clear(); // Removes all elements from the numlist. //Removes all Strings from the list. List<String> namelist = new List<String>('Malli','Hari','Krish'); Namelist.clear() //Removes all names from the namelist.
Clone Method: Creates a new list and adds all the elements of an existing list into that new list.
// Clone the integer Elements List<Integer> numlist = new List<Integer>(20,10,30); List<Integer> newnumlist = numlist.clone(); //Creates new list called newnumlist and adds all the elements into it from numlist. // Clone the String Elements List<String> namelist = new List<String>('Nagarjun','Hari','malli'); List<Integer> newnamelist = namelist.clone(); //Creates new list called newnumlist and adds all the elements into it from namelist.
Note: If this method is used on List of Sobject records it will do only perform a shallow clone.
List<Account> acclist = new List<Account>(a1,a2,a3); List<Account> newacclist = acclist.clone() //Creates a new list newacclist and virtually creates the new elements a1,a2,a3 into it.
Deepclone Method: Creates a new list and adds all the elements of an existing list into that new list. This method can be used only for Sobject lists and not for primitive lists.
// Deeclone the s-objects List<Account> acclist = new List<Account>{a1,a2,a3}; List<Account> newacclist = acclist.deepclone() //Creates a new list newacclist and actually adds the new elements a1,a2,a3 into it.
IsEmpty Method: Boolean method which returns true if the list has no elements.
// Clear the List List<String> namelist = new List<String>{'Hari','Rakesh','Suneel'}; namelist.clear(); //As all the elements are now removed from the namelist using the clear method.
Size Method: Return the number of the elements stored in the list.
// Size of the List List<String> namelist = new List<String>{'Hari','Rakesh','Suneel'}; Integer I = namelist.size(); // integer I have size of the list, Number of elements in namelist are 3
// Sort the element in the list List<String> namelist = new List<String>{'Nagarjun','Malli','kishor'}; namelist.sort();