Tuesday 17 September 2013

Collections


MAP

public with sharing class MapUsage {
       
        public static void mapUse(){
       
            Map<Integer, String> mp = new Map<Integer, String>();
            // kEy is Integer
            // value is String
           
            mp.put(1221,'Mahi');
            //mp.put(1226,'Shyam');
            mp.put(1211,'Rajesh');
            mp.put(1255,'Kiran');
           
            System.debug('---------------Elements in the map are ------------>'+mp);
            System.debug('---------------Size of the map is-------4---------->'+mp.size());
            System.debug('------------Keys in the map are ----------->'+mp.keyset());
            System.debug('------------Values in the map are ----------->'+mp.values());
           
            System.debug('------------------'+mp.get(1211));
           
            mp.remove(1226);
           
            mp.put(1211,'Charan');
            mp.put(1248,'Pavan');
            mp.put(1222,'Kiran');
           
            System.debug('---------------Elements in the map are ------------>'+mp);
            System.debug('---------------Size of the map is-------5---------->'+mp.size());
           
            for(Integer i : mp.keyset()){
                System.debug('-----Key--------'+i);
                System.debug('-----value---------'+mp.get(i));
            }
           
            for(String s: mp.values()){
                System.debug('----------------'+s);
            }
           
        }       
}

SET
Set usage in APEX:


public with sharing class SetUsage {
   
    public void setUse(){
       
        Set<Integer> st = new Set<Integer>();
       
        st.add(1234);
        st.add(1248);
        st.add(1225);
        st.add(1252);
       
        System.debug('----------------Elementa in the set are --------------'+st);
        System.debug('----------------Size of Elementa in the set are ------4--------'+st.size());
       
        st.remove(1252);
        System.debug('----------------Size of Elementa after removing an element in the set are ------3--------'+st.size());
       
       
        st.add(1248);
        st.add(1252);
        st.add(1221);
        System.debug('----------------Size of Elementa in the set after adding duplciates are ------5--------'+st.size());     
       
        Set<Integer> newst = st.clone();
        System.debug('----------------Size of Elementa in the new set are -------5-------'+newst.size());
       
        st.clear();
        System.debug('-----------------NEw set is EMpty or not--------false--------'+newst.isEmpty());
       
        for(Integer i: newst){
            System.debug('----------------->'+i);
        }       
    }
   
}


LIST

public with sharing class ListUsage {
       
        public void listUse(){
           
            // Initialising a list
            List<String> lst = new List<String>();
           
            // Adding elemenst into list
            lst.add('ABC');
            lst.add('DEF');
            lst.add('GHI');
            lst.add('JKL');
           
            System.debug('---------Elements in the list are-------------->'+lst);
            System.debug('----------Size of the Elements are-----4-------->'+lst.size());
            System.debug('-----------Element in 2nd index--------GHI-------->'+lst[2]);

            lst.remove(1);
            System.debug('---------Elements in the list after removing are-------------->'+lst);
            System.debug('----------Size of the Elements after removing are----3--------->'+lst.size());
           
            lst.add('MNO');
            lst.add('GHI');
            lst.add('DEF');
            lst.add('1248');
           
            System.debug('---------Elements in the list after adding duplicate values are-------------->'+lst);
            System.debug('----------Size of the Elements after adding duplicate values are--------7----->'+lst.size());
           
            List<String> newlst = lst.clone();
            System.debug('---------Elements in the new list are-------------->'+newlst);
            System.debug('----------Size of the Elements in new list are-------7------>'+newlst.size());
           
            lst.clear();// removes all the elements in the list
            System.debug('-----------------List is Empty or not-------true----------->'+lst.isEmpty());
            System.debug('-----------------New List is Empty or not-------false----------->'+newlst.isEmpty());
           
            //Indexing for loop
            for(integer i=0; i<newlst.size(); i++){
                System.debug('-----Index------->'+newlst[i]);
            }
           
            //For Each Loop
            for(String s: newlst){
                System.debug('-----For Each---------->'+s);
            }
           
           
        }
       
       
       
}


No comments:

Post a Comment