How to use list methods in salesforce

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.


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

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

  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,

    List<String> name = new List<String>();

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>();
add(index, ListElement); It inserts the element at the given index number.

        List<String> name = new List<String>();

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.

    By Posted On

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to create Master-detail relationship in Salesforce? Raja Gulla

In Salesforce, relationships are used to create a link between objects. Whenever user clicks on particular object they can view its data and related data ...

Continue Reading...
Approval process in Salesforce Raja Gulla

As we know there will be role hierarchy in many organizations and permissions are assigned according to their roles. So, When a record or data ...

Continue Reading...
How to create reminder or automatic reminder in salesforce Raja Gulla

In salesforce organization, when you have to work on lot of leads and each lead will be at different stage. To make it easier to ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties