Salesforce Apex Collections: List & List Methods with Examples


List is a collection of simillar Datatype elements .DataType can be any thing that is supported by apex (Primitive,Sobjects, collections, Wrapper classes..). The  size of the list can grow or reduce based on run time requirement .

-Elements in the list maintains insertion order .
- list allows Duplicate elements .
-In the salesforce we have predefined apex class List .
-There are some predefined methods to support list operations.

Syntax for List Creation:

 List<DataType> listName =new List<DataType>();

Example:

List<Integer> ages=new List<Integer>();

Salesforce Apex List Methods with Examples

add(element) : This method is used to add new element into the list at the last position .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(20);

System.debug(ages); // { 10,20,20}


add(index,element) : This method is used to add new Element at the given index. Index value in the list starts with  zero

     0    1     2
------------------
| 10 |  20 | 20  |
------------------

ages.add(2,60);

  0    1     2    3
-----------------------
| 10 | 20  | 60 | 20  |
-----------------------

System.debug(ages); //  { 10,40,20,30 }

addAll(List|Set) :  This method is used to add set|List of elements into the list.

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

System.debug(ages); //  { 10,20,30 }

List<Integer> myList=new List<Integer>();

myList.add(10);

myList.add(90);

myList.add(40);

System.debug(myList); // { 10,90,40}

myList.addAll(ages);  // All  the elemenets which are in ages are added to myList.

System.debug(myList); // { 10,90,40,10,20,30 }


get(index) : This method will return the element which is at the given index .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

ages.add(40);

System.debug(ages);  // {10,20,30,40 }

 0    1     2   3
---------------------
| 10 | 20 | 30 | 40 |
---------------------

Integer a=ages.get(2);

System.debug(a); // 30

Integer b=ages.get(3);

System.debug(b); // 40

Integer c=ages.get(0);

System.debug(c); // 10

remove(index) : This method will remove the element at the given index .

Example :  List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(30);

ages.add(40);

ages.add(90);

System.debug(ages);  // { 10,30,40,90 }

 0      1      2     3
---------------------------
| 10  |  30  |  40  | 90  |
---------------------------

ages.remove(2);

System.debug(ages); //  { 10, 30, 90}

 0      1     2
-------------------
| 10  |  30  | 90 |
-------------------

ages.remove(1);

System.debug(ages); // { 10,90 }

  0      1
--------------
|  10   | 90  |
--------------
clear() : This method will remove all the elements in the list .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

System.debug(ages); // { 10, 20,30 }

ages.clear();

System.debug(ages); // {}

sort() : This method is used to sort the elements in the ascending order .

Example :

List<Integer> ages = new List<Integer>();

ages.add(10);

ages.add(90);

ages.add(30);

ages.add(70);

System.debug(ages); //  { 10,90,30,70 }

ages.sort();

System.debug(ages); //  { 10,30,70,90 }


clone() : This will create a duplicate copy of list of elements

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

List<Integer> values=ages.clone();

System.debug(ages); // { 10,20,30 }

System.debug(values); // { 10,20,30}

values.add(90); // Changes made on values will not effect ages

System.debug(values); // { 10,20,30,90}

System.debug(ages); // {10,20,30} // Changes made on ages will not effect values.


deepClone() : Makes a duplicate copy of a list of sObject records,
including the sObject records themselves

Example : List<Account> accs= new List<Account>();

Account a1=new Account(Name='Wipro',Industry='Banking');

accs.add(a1);

Account a2=new Account(Name='TCS',Industry='Energy');

accs.add(a2)

System.debug(accs); // {  Account :{Name=Wipro ,Industry=Banking } ,

 Account :{Name =TCS ,Industry=Energy} }

}

List<Account> dupLicates=accs.deepClone();

System.debug(dupLicates); // {  Account :{Name=Wipro ,Industry=Banking } ,

  Account :{Name =TCS ,Industry=Energy} }

     }


Notes : Any changes made on accs elements will also be applied on duplicates


size() : This will return no of elements in the list .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

Integer count =ages.size();

System.debug(count); // 3


 set(Index,ele) : This will replace  the element at the given index with value .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

ages.add(40);

System.debug(ages); // {10,20,30,40 }

ages.set(1,90);

System.debug(ages); // { 10,90,30,40 }

ages.set(3,55);

System.debug(ages); // {10,90,30,55}

Post a Comment