List methods in Salesforce with interesting examples







List
  • A list is an ordered collection of elements. 
  • List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types
  • List allows duplicate value
Syntax: 

List<datatype> variablename = new List<datatype>();

Example:
 

  List<String> bollywoodActresses= new List<String{'Deepika','Anushka','Alia'};
  
Hence values are stored as,
      index postion 0 = Deepika
      index postion 1 = Anushka
      index position 2 = Alia

In another way, we can assign values dynamically when we are creating a list.

Methods in list
There are few methods available in list
add(list Element)
add(index, list Element)
addAll(from List)
addAll(from Set)
clear()
clone()
remove()
size()
equals(list2)
get()
sort()
hashCode()
isEmpty()

 add(index,ListElement)
 It inserts an element into the list at the specified index position. 


        List<string> bollywoodActresses= new List<string>();
        name.add(0,'Anushka');
        name.add(1,'Deepika');




addAll(fromList)

 Adds all of the elements in the specified list to the list that calls the method. Both lists must be of the same type. 



   List<string> bollywoodActress = new List<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       System.debug(bollywoodActress);
       List<string> bollywoodActor = new List<string>();
       bollywoodActor.add('Salman');
       bollywoodActor.add('Ranveer');
       bollywoodActor.add('Ranbir');
       System.debug(bollywoodActor);
       bollywoodActor.addAll(bollywoodActress);
       system.debug('Both list names are '+' '+bollywoodActor+'');



addAll(fromSet)

It is used to add all the elements in the set when this method is called, and both set and list elements should be of same type.  



List<string> bollywoodActress = new List<string> {'Deepika', 'Kareena','Alia'};
Set<string> bollywoodActor = new Set<string> {'Salman', 'Ranveer', 'Ranbir'};
        bollywoodActress.addAll(bollywoodActor);
        system.debug(bollywoodActress);
   



size()
It is used to get the total number of elements in the list.

 Example: bollywoodActor.size();

remove()
It removes the list element stored at specific index, returning the element that was removed 

   List<string> bollywoodActress = new List<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
      System.debug(bollywoodActress);
       bollywoodActress.remove(2); //remove 2nd position value.
      System.debug(bollywoodActress);

sort()
This method s sorts list in ascending order. 

List<string> bollywoodActress = new List<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       System.debug(bollywoodActress);
       bollywoodActress.sort(); //sort in ascending order
       System.debug(bollywoodActress);

clear()
It will remove all the elements from the list. Hence the length of that list will be zero. 

  List<string> bollywoodActress = new List<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       System.debug(bollywoodActress);
       bollywoodActresses.clear();
       System.debug(bollywoodActress);

get(index) 
It returns the element which is at given index. 

bollywoodActresses.get(1)

clone() 
It creates another copy of the list.
  List<string> bollywoodActress = new List<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       System.debug(bollywoodActress);
  List<string> secondList = new List<string>();
      secondList=bollywoodActress.clone();
      System.debug(secondList);

equals(List)
Compares the list with the specific list and returns true if both are equals, otherwise, returns false.

 List<string> bollywoodActress = new List<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       System.debug(bollywoodActress);
      
       List<string> bollywoodActor = new List<string>();
       bollywoodActor.add('Salman');
       bollywoodActor.add('Ranveer');
       bollywoodActor.add('Ranbir');
       System.debug(bollywoodActor);
       Boolean result;
       result=bollywoodActor.equals(bollywoodActress);
       system.debug(result);

hashCode()
Returns the hash code corresponding to this list and its contents. 

 List<string> bollywoodActor = new List<string>();
       bollywoodActor.add('Salman');
       bollywoodActor.add('Ranveer');
       bollywoodActor.add('Ranbir');
       System.debug(bollywoodActor);
       System.debug(bollywoodActor.hashCode());
//Output: 485459500

isEmpty()
True if the list has zero elements.

List<string> bollywoodActor = new List<string>();
       bollywoodActor.add('Salman');
       bollywoodActor.add('Ranveer');
       bollywoodActor.add('Ranbir');
     System.debug(bollywoodActor);
  System.debug(bollywoodActor.isEmpty());
//Output: false

 There is no limit for a list to hold any number of records. But there is a limit for heap size, which is 6 Mb for synchronous and 12 Mb for asynchronous.

Share this

Related Posts

Previous
Next Post »