The collection is the type of variable that can store multiple numbers of records. It can increase and decrease dynamically.
There are basically, 3 types of collection – List, Set and Map.
List: It is an ordered collection of primitives, sObjects, collections, or Apex objects based on indices as well as List allows duplicacy of the elements.
Set: An unordered collection of unique primitives, sObjects, collections, or Apex objects as well as Set won’t allows duplicacy of the elements.
Map: A collection of primitive unique keys that map to single values which can be primitives, sObjects, collections, or Apex objects.
Note : – Collections do not support adding or removing elements/values while iterating over the collections.
List can contain any number of records of primitive, collections, sObjects, user defined and built in Apex type. This is one of the most important type of collection and also, it has some system methods which have been tailored specifically to use with List. List index always starts with 0. This is synonymous to the array in Java. A list should be declared with the keyword 'List'.
- List can have duplicate entries. The integer 10 is added twice to the list.
- List is a ordered collection, meaning that each element is stored in a specific position. An element can be accessed using its location (also called as Index). The index always starts with 0.
The syntax to create a new list is as follows:
List<DataType> listName = new List<DataType>();
Here, DataType should be replaced by the type of data you would like to store and listName should be replaced by the name of the list. For example, if I want to create a list of integers, I can create it as follows:
List<Integer> numbers = new List<Integer>();
The above code snippet will create a list of Integers named as numbers. In this list you can store any number of integers. If you want to add elements to the list at the same moment when a list is initialized, you can do that as well. Consider the below examples:
1. Creating a list of Integers
Code Snippet:
List<Integer> numbers = new List<Integer>{1,2,3,4,5}; System.debug(numbers);
Output:
2. Creating a list of Strings
Code Snippet:
List<String> names = new List<String>{'Richard', 'Gilfoyle', 'Dinesh', 'Jared', 'Monica'}; System.debug(names);
In both the above examples, you can see that we've used curly braces {} to specify a comma separated set of values and that values are automatically added to the list. Using this way, you can initialize a list with a set of values.
Example
Below is the list which contains the List of a primitive data type (string), that is the list of cities.
List<string> ListOfCities = new List<string>(); System.debug('Value Of ListOfCities'+ListOfCities);
Declaring the initial values of list is optional. However, we will declare the initial values here. Following is an example which shows the same.
List<string> ListOfStates = new List<string> {'NY', 'LA', 'LV'}; System.debug('Value ListOfStates'+ListOfStates);
List of Accounts (sObject)
List<account> AccountToDelete = new List<account> (); //This will be null System.debug('Value AccountToDelete'+AccountToDelete);
We can declare the nested List as well. It can go up to five levels. This is called the Multidimensional list.
This is the list of set of integers.
List<List<Set<Integer>>> myNestedList = new List<List<Set<Integer>>>(); System.debug('value myNestedList'+myNestedList);
List can contain any number of records, but there is a limitation on heap size to prevent the performance issue and monopolizing the resources.
Methods for Lists
There are methods available for Lists which we can be utilized while programming to achieve some functionalities like calculating the size of List, adding an element, etc.
Following are some most frequently used methods −
- size()
- add()
- get()
- clear()
- set()
The following example demonstrates the use of all these methods
// Initialize the List List<string> ListOfStatesMethod = new List<string>(); // This statement would give null as output in Debug logs System.debug('Value of List'+ ListOfStatesMethod); // Add element to the list using add method ListOfStatesMethod.add('New York'); ListOfStatesMethod.add('Ohio'); // This statement would give New York and Ohio as output in Debug logs System.debug('Value of List with new States'+ ListOfStatesMethod); // Get the element at the index 0 String StateAtFirstPosition = ListOfStatesMethod.get(0); // This statement would give New York as output in Debug log System.debug('Value of List at First Position'+ StateAtFirstPosition); // set the element at 1 position ListOfStatesMethod.set(0, 'LA'); // This statement would give output in Debug log System.debug('Value of List with element set at First Position' + ListOfStatesMethod[0]); // Remove all the elements in List ListOfStatesMethod.clear(); // This statement would give output in Debug log System.debug('Value of List'+ ListOfStatesMethod);
You can use the array notation as well to declare the List, as given below, but this is not general practice in Apex programming −
String [] ListOfStates = new List<string>();