2018 Jan -Collection In Salesforce


Harry: A for Apple ,B for Ball , C for Cat.....

Adam : Harry, Are you Okay..?

Harry: Yes I'm Okay, just preparing myself how to learn Apex collection from begining. :-)

Adam: Ohhh Man...Seriously!!!
No need to worry just turn off your mobile & sit with me, we will go together using Develope console.

Adam: Basically three types of collection: List, Set, Map: 


*List in Salesforce :

List :  List is collection of an ordered set of elements.which can contain duplicate items.
Example:
//Creation of list
List<String> lst= new List<String>();
lst.tempVeri=’mayur’;
system.debug(‘list First element :’+ tempVeri.get(0));

Methods in list
1    Add
2    Add(1,20)   which means at index value of 1 add 20.
3    addAll.

Example:
List<Integer> v1= new List<Integer>();
v1.add(10);
v1.add(20);
System.debug('V1 0 , v1 1 place values: '+V1);
List<Integer> v2 = new List<Integer>{30,40};
v1.addAll(v2); 
System.debug('final list is here: '+V1);

               O/P:
                    DEBUG|V1 0 , v1 1 place values: (10, 20)
final list is here: (10, 20, 30, 40)


     4.Remove
List<Integer> v1= new List<Integer>();
v1.add(10);
v1.add(20);
System.debug('V1 0 , v1 1 place values: '+V1);
List<Integer> v2 = new List<Integer>{30,40};
v1.addAll(v2); 
System.debug('final list is here: '+V1);

v1.remove(0); 
System.debug('final list is here: '+V1);

            O/P: DEBUG|final list is here: (10, 20, 30, 40)
                     |final list is here: (20, 30, 40)                            

5   Equals(list)
Example:
List<Integer> v1= new List<Integer>();
v1.add(10);
v1.add(20);
System.debug('V1 values: '+V1);
List<Integer> v2 = new List<Integer>{10,20};
System.debug('V2 values '+V2);
Boolean t= v1.equals(v2);
system.debug(t);

     O/P:
             DEBUG|V1 values: (10, 20)
             DEBUG|V2 values (10, 20)
             DEBUG|true


isEmpty
Example:
List<Integer> v1= new List<Integer>();
Boolean t1= v1.isempty();
system.debug(t1);
v1.add(10);
v1.add(20);

System.debug('V1 values: '+V1);
Boolean t= v1.isempty();
system.debug(t);

      O/P:
              DEBUG|true
              DEBUG|V1 values: (10, 20)
              DEBUG|false


6    Size
List<Integer> v1= new List<Integer>();
v1.add(10);
v1.add(20);
Integer count =v1.size();
System.debug('Size'+Count);

      O/P:
             Size 2

7    Clear();
List<Integer> v1= new List<Integer>();
v1.add(10);
v1.add(20);
System.debug(v1);
v1.clear(); 
System.debug(v1);

      O/P:
 |DEBUG|(10, 20)
 DEBUG|()

8    Clone()
List<Integer> v1= new List<Integer>();
v1.add(10);
v1.add(20);
System.debug(v1);
List<Integer> v2= v1.clone();
System.debug(v2);

      O/P:
            DEBUG|(10, 20)
            DEBUG|(10, 20)

9.sort()
List<Integer> v1= new List<Integer>();
v1.add(100);
v1.add(40);
v1.add(30);
v1.add(50);
System.debug(v1);
v1.sort();
System.debug(v1);

      O/P:
            DEBUG|(100, 40, 30, 50)
            DEBUG|(30, 40, 50, 100)


*Set in Salesforce
Set is an unordered collection in which duplication of record is not allowed.
Unlike to list there is element can not be identified with help of indexing.
set<Integer> test1= new set<Integer>{20,30,10,40,40,50};
system.debug('Value at 0rth : '+test1);

      O/P :
           10 20 30 40 50

Methods are similar as List.


 *Map in Salesforce
Map is collection class which in holds group of an element. A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types

Map<Integer,String> test2 = new Map<Integer,String>();
test2.put(1,'mayur');
test2.put(2,'Solanki');
System.debug(test2.get(1));
Map<String,Integer> test1 = new Map<String,Integer>();
test1.put('mayur',1);
test1.put('Solanki',2);
System.debug(test1.get('mayur'));
if(test2.size()!=0){
    system.debug('Awesome');
}
if(!test1.isEmpty()){
    system.debug('Learning');
}

           O/P:
                   DEBUG|mayur
                   DEBUG|1
                   DEBUG|Awesome

                   DEBUG|Learning

Another Example:
Map is collection of key value pair.
Suppose you are in Hotel & Hotel has different room .Each Room has Room Number from where they are identified.

Map<Integer,String> mapExample= new Map<Integer,string)();
mapExample.put(101,FirstRoom);
System.debug(‘My booking details: ’+mapExample);

Second example:
Map<Integer,String> mapExample= new Map<Integer,String>();
mapExample.put(101,'MybookedRoom');
mapExample.put(102,'Coorg');
mapExample.put(103,'Ooty');
system.debug('room details: '+mapExample.get(102));

          O/P:
              DEBUG|room details: Coorg

Comments

Popular posts from this blog

LearnSalesforce

LWC Interview Questions Part-1

Lightning Web Component