Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Salesforce

How to Use List Methods in Salesforce

2022-07-20

List is a collection of elements, and they are ordered according to their insertion; elements can be of any data-type. List allows duplicate values and are referred by their indices. Output of every SOQL(Salesforce Object Query Language) query is a list.

Syntax:

Creating a list: List<DataType> variablename = new List<DataType>();

We can assign the values statically at the time of creating the list,

Ex:
  List<String> name = new List<String>{'John','Kelly','Mike'};

Hence values are stored as,

index pos 0 = John
      index pos 1 = Kelly
      index pos 2 = Mike

In another way, we can assign values dynamically when we are creating a list,

Ex:
    List<String> name = new List<String>();
    name.add('John');
    name.add('Kelly');
    name.add('Mike');

Hence the values and index positions are stored as mentioned above.

List has many predefined methods in which we will look into some of them.

add(ListElement): It inserts an element into the list.     

List<String> name = new List<String>();
      name.add('cat');
      name.add('sri');

add(index, ListElement); It inserts the element at the given index number.

List<String> name = new List<String>();
        name.add('John');
        name.add('Kelly');
        name.add(0,'Mike');
        name.add(1,'Paul');

Positions will be like, 
         index pos 0=Mike
         index pos 1=Paul
         index pos 2=John
         index pos 3=Kelly

addAll(fromList): It is used to add all the elements in the list specified, and both lists should be of same type. It is represented as,

Public void addAll(List fromList)

addAll(fromSet): It is used to add all the elements in the set when this method is called, and both set and list elements should be of same type. Represented as,

Public void addAll(Set fromSet)

size(): It is used to get the total number of elements in the list. Represented as,

Public integer size()

clear(): It will remove all the elements from the list. Hence the length of that list will be zero.

Public void clear()

get(index): It returns the element which is at given index.

Public object get(Integer index)

To reference an element in the list, we can also use the element index position in square brackets such as,

List<data type> name = new dataype[index position]

isEmpty(): It returns true if the list is empty.

Public Boolean isEmpty()

clone(): It creates another copy of the list.

Public List<object> clone()

If the list is of sObject records, the duplicate list can only be a copy of the list. Only the duplicate will have references to each object, but the sObject records themselves will not be duplicated. To duplicate these sObject records, we have to use deepClone method.  

Note: There is no limit for a list to hold any number of records. But there is a limit for heap size, which is 6 Mb for synchronous and 12 Mb for asynchronous.

Above-mentioned methods are some of them and there are other methods.