Sunday 21 February 2016

LIST

------------------------------------------------------
List  :
-------------------------------------------------------
1.List is a collection of simillar elements

2.Size of the list can increase or decrease based on runtime requirement

3.Elements in the list are reffered using index

4.List will accept duplicate values .

5.List will maintain insertion order


Syntax :
List<DataType> listName ;

  Example :

List<Integer> ages;
List<String>  names;
List<Account> accs;
List<Contact> cons;

System.debug(ages); // null as no memory is allocated

6.Assigning the Memory for array

List<Integer> ages=new List<Integer>();
List<String> names=new List<String>();
List<Account> accs=new List<Account>();
List<Contact> cons=new List<Contact>();
List<Opportunity> oops=new List<Opportunity>();

7. Static Allocation of list values

Syntax :
List<Datatype> lstname=new List<DataType>{val1,va2...};

  Example 1: Create a list of integer values and assign values

List<Integer> ages=new List<Integer>{10,20,30};
  0      1      2
----------------------
|  10  |  20   |  30 |
        ----------------------

  Example 2: Create a list of strings and assign the value

List<String> names=new List<String>{'Ram','Kiran','Hari'};

  0       1       2
  -------------------------
|  Ram  | Kiran  | Hari |
        -------------------------

  Example 3:Create a list of account sobject and add the elements
   statically

Account a1=new Account(Name='Wipro',Phone='123');
Account a2=new Account(Name='Tcs',Phone='456');

List<Account> accs=new List<Account>{a1,a2};

0 1
------------------------------------------------------------------
| Account:{Name:Wipro ,Phone=123} |Account:{Name:tcs phone=456 } |
------------------------------------------------------------------


8. List Methods

  1. add(ele) : This will add  new element to the list

  Example 1: Create list of integers and three four values and
    display
     List<Integer> ages=new List<Integer>();
     ages.add(10);  
     ages.add(20);
     ages.add(30);
     ages.add(30);

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

for(Integer a: ages){
System.debug(a);  //10
}

0      1    2    3
------------------------
| 10  | 20  | 30  | 30 |
        -------------------------   // a=10
|
a
For Loop :

  0      1    2    3
------------------------
| 10  | 20  | 30  | 30 |   // a=20
        -------------------------
|
a

         0      1    2    3
------------------------
| 10  | 20  | 30  | 30 |   // a=30
        -------------------------
    |
    a


0      1    2    3
------------------------
| 10  | 20  | 30  | 30 |   // a=30
        -------------------------
       |
           a

 Example 2:Create a list of account sobject and  few records to it

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

   Account a1=new Account(Name='Wipro',Phone='123');
 
   accs.add(a1);

   Account a2=new Account(Name='TCS',Phone='456');
   accs.add(a2);

    for(Account a:accs){
System.debug(a.name);
System.debug(a.phone);

    }

------------------------------------------------------------------
| Account :{Name:Wipro ,Phone:123} | Account:{Name:TCS,Phone:456}|
-----------------------------------------------------------------
   |
   a

System.debug(a.name) ;// Wipro

System.debug(a.phone); // 123

------------------------------------------------------------------
| Account :{Name:Wipro ,Phone:123} | Account:{Name:TCS,Phone:456}|
-----------------------------------------------------------------
    |
    a

System.debug(a.name); //TCS

System.debug(a.phone); // 456


 Example 3: Create list of opportunity and add three opportunity
 records with (name,amount) fields and display the values

List<opportunity> oops=new List<Opportuity>();

Opportunity op1=new Opportunity(Name='Admin',amount=1000);
Opportunity op2=new Opportunity(Name='Dev',amount=50000);
Opportunity op3=new Opportunity(Name='Test',amount=60000);

oops.add(op1);
oops.add(op2);
oops.add(op3);

for(Opportunity op: oops){
System.debug(op.name);
System.debug(op.amount);

}

  2.add( index ,ele) :
This method wil add element at given index


List<Integer> ages=new List<Integer> {10,20,30};
           0    1     2
------------------
| 10  | 20 | 30 |
        -----------------

ages.add(40);

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

ages.add(2,50);

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

ages.add(1,90);

 0    1     2     3    4   5
--------------------------------
| 10 | 90  | 20 | 50 |  30 | 40 |
        ---------------------------------

 3. addAll(set| list)

List<Integer>  marks=new List<Integer>{10,30,50};

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

values.add(20);
values.add(30);
----------
|20 | 30 |
----------
values.addAll(marks);

-------------------------
        | 20 | 30 | 10 | 30 |50|
        ------------------------

 4. get(index)

List<Integer> ages=new List<Integer>{10,30,50};

 0    1    2
---------------
| 10 | 30 | 50|
       -----------------

Integer a=ages.get(2);

System.debug(a); // a=50

List<Account > accs=new List<Account>();
Account a1=new Account(Name='capital',Phone='888');
Account a2=new Account(Name='Saas',Phone='777');
Account a3=new Account(Name='Wipro',Phone='666');
accs.add(a1);
accs.add(a2);
accs.add(a3);

Account a=accs.get(1);

System.debug(a.name); // Saas
System.debug(a.phone); //777

 4. remove(index) :

List<Integer> ages=new List<Integer>{10,20,30,40};

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

ages.remove(1);

          0    1    2
---------------
| 10 | 30| 40 |
       ----------------
   
ages.remove(2);

  0    1    
------------
| 10 |  30|
       -------------
  5. clear() :

This will remove all the elements from the list

List<Integer> ages=new List<Integer>{10,20,30,40};

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

ages.clear() ; This will delete all the elements

System.debug(ages); //{}

   6. isEmpty();

if the list is empty it will return true


List<Integer> ages=new List<Integer>{10,20};

System.debug(ages.isEmpty()); //false

ages.clear();

System.debug(ages.isEmpty()); true

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

List<Integer> ages=new List<Integer>{10,20,30};

Integer size=ages.size(); // 3

  8. sort() : This will sort all the elements in the list
   















No comments:

Post a Comment