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.

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.

Posted On 01 February 2018 By MicroPyramid


Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to use custom labels in salesforce

Salesforce is used by many users whose native languages are different, If the application doesn't have any Multilanguage feature, it would be difficult for the ...

Continue Reading...
Approval process in Salesforce

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 one to one relationship between objects in salesforce

Salesforce provides two relationships known as one to many relationship and many to many relationship(this can be done using junction object). Hence in this, we ...

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