Sunday, March 10, 2013

Collections (List,Set,Map) in Salesforce

List:

public class listCls{
public void m1(){
List<String> lststr = new List<String>();
lststr.add('A');
lststr.add('B');
lststr.add('C');
lststr.add('D');
System.debug('-------------->Liststr Size'+ lststr.size());
System.debug('-------------->4 th Element'+ lststr[3]);
List<String> newlst = lststr.clone();
System.debug('-------------->New List size'+ newlst.size());
lststr.clear();
System.debug('-------------->After Clearing'+ lststr.size());
System.debug('--------------->List Empty'+ lststr.isEmpty());
newlst.sort();
for(integer i=0; i<newlst.size();i++){
System.debug('---------->'+newlst[i]);
                                                     }
for(string s: newlst){
System.debug('---------->'+s);
                             }
                          }
                            }

Set:

public class SetCls{
    public void m1(){
        Set<String> setstr = new Set<String>();
        setstr.add('A');
        setstr.add('B');
        setstr.add('C');
        setstr.add('D');
        System.debug('-------------->Setstr Size'+ setstr.size());
        System.debug('-------------->4 th Element'+ setstr.contains('D'));
        Set<String> newset = setstr.clone();
        System.debug('-------------->New Set size'+ newset.size());        
        newset.add('A');
        System.debug('-------------->New Set size'+ newset.size());        
        setstr.clear();
        System.debug('-------------->After Clearing'+ setstr.size());
        System.debug('--------------->Set Empty'+ setstr.isEmpty());
        
        for(string s:newset){
            SYstem.debug('---------->'+s);
                                     }       
                                }
                                 }

Map:

public class mapcls{
public void m1(){
/*
Map<Integer, string> mp = new Map<Integer, string>();
mp.put(1,'ABC');
mp.put(2,'DEF');
mp.put(5,'GHI');
System.debug('------------->Key values in MAp'+ mp.keyset());
System.debug('------------->Key values in MAp'+ mp.values());
System.debug('-------------->Value of 2nd key is: '+ mp.get(2));
Map<Integer, string> newmp = mp.clone();
System.debug('------------->New map'+ newmp);
mp.clear();
System.debug('---------------->'+mp.size());
System.debug('---------------->'+mp.isEmpty());
*/
List<string> lst= new List<String>();
lst.add('A');
lst.add('B');
lst.add('C');

List<string> lst1= new List<String>();
lst1.add('D');
lst1.add('E');
lst1.add('F');

Map<Integer, List<String>> mplst = new Map<Integer, List<String>>();
mplst.put(1,lst);
mplst.put(2,lst1);

System.debug('---------------->Keys---'+mplst.keyset());
System.debug('---------------->Values--'+mplst.values());
System.debug('----------------->'+mplst.get(2));
List<string> newlst = mplst.get(2);
System.debug('----------->'+newlst);
                       }
                         }

No comments:

Post a Comment