APEX COLLECTION : List

Salesforce Apex Collection : List

In previous Episode we have discussed how to use array [] in an apex.

In this episode, we are going to discuss a very important topic from apex i.e. salesforce collections.
Before starting, Let's see how many collection types does salesforce support.

Salesforce support 3 types of collections in Salesforce
1. List
2. Set
3. Map


In this episode, we are focusing on List.
So when you have array[] in apex why you will use collections?

Let's understand the difference first:

Array 
Array is a collection of homogeneous (similar) elements.
- Array cannot grow and shrink dynamically.
- Array can be accessed faster and less memory.

Collections :
- It is a collection of homogeneous(similar) and heterogeneous (non-similar) elements.
- It can grow and shrink dynamically.
- Collections are slow compared to an array and consume more memory.
So let's get started with List :

List
:
List is an Interface.

A list is an ordered collection of elements that are distinguished by their indices.
List elements can be of any datatype - primitive types, collections, Sobjects, user-defined types and built-in apex type.

For  example, consider an old tiffin box where tiffins are arranged with ordered data look at at this figure 
                          salesforce apex collections list

As I have mentioned before List is ordered the collection of elements.

That means I have created a Green record first then Blue record then Yellow then Red record as you can see in the above figure.
Hence when you use a list then it will give you an ordered collection of elements like Green, Blue, Yellow, Red.

In List, Insertion order is preserved.

It can grow dynamically at run time.

Duplicate values are allowed.

Null values are accepted.
Now Let's understand the syntax of list :
Syntax :
=============================================
List<Data Type> ListName = new List<Data Type>();

For Example:
                      List<string> mylist = new List<string>();
=============================================

In the above syntax, you can easily understand that by using List<> you are defining list then you are also defining what this list contains by mentioning Data Type  inside < >.

Then you are mentioning list name i.e. instance name which you are further using in your code to reference this list.

Then you are using New keyword to create a new list.
That's it !! you are good to go now.

Next important part is List Methods.

These methods you will use for performing a certain action on your newly created list.

Let's understand the methods in list class :

add(object) : 
Adds an element to the end of the list.

add(Integer, Object) : 
Inserts an element into the list at the specified index position.

addAll(List) : 
Adds all of these elements in the specified list to the list that calls the method. The set and the list must be of the same type.

clear() :
Removes all elements from a list, Consequently setting the list's length to zero.

clone() :
Makes a duplicate copy of a list.

deepClone(Boolean, Boolean, Boolean) :
Makes a duplicate copy of the list of sObject records themselves.

equals(List) :
Compares this list with the specified list and returns true if both lists are equal. Otherwise returns false.

get(Integer) :
Returns the list element stored at the specified index.

getsObjectType() :
Returns the token of the sObject type that makes a list of sObjects.

hashCode() :
Returns the hashcode corresponding to this list and its contents.

isEmpty() :
Returns true if the list has zero elements.

iterator() :
Returns an instance of an iterator for this list.

remove(Integer) :
Removes the list element stored at the specified index, returning the element that was removed.

set(Integer, Object) :
Sets the specified value for the elements at the given index.

size() :
Returns the number of elements in the list.

sort() :
Sorts the items in the list in ascending order.
Coool !! right ? so many methods right ? but no need to remember all these methods just put " . " after your list instance name then automatically all these methods comes up.

Now let's cook this recipe with the help of an example :
=============================================
List<string> str = new List<string>
       string s1 = "sam" ;
       string s2 = "ram" ;
       string s3 = "ravi" ;
     
       str.add(s1);  //adds "sam" in s1
       str.add(s2); //adds "ram" in s2
       str.add(1,s3); //adds "ravi" in s3 ,further add value of s3 in 1

List<string> findlist = new List<string>() ;
  findlist.addAll(str) ;
  string x = str.get(1) ; //Ravi
  
return x ; //return value inside x i.e. Ravi 
=============================================

In the above example, we have created str list and findlist list.

In str, we have defined string s1,s2,s3 then by using add method it adds s1,s2,s3.

Then in the second list, we add str in second list findlist.

Then by using get(1) method, it will get value inside 1 and store inside x and return finally x i.e. Ravi.

That's it !! In this way, you can use the apex collection List.

WOHOOO !! YOU HAVE JUST COMPLETED APEX COLLECTION: LIST EPISODE 
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
Happy learning ☁️⚡️ (Learn. Help. Share.)


<< PREVIOUS                                                      NEXT >>


APEX COLLECTION : List APEX COLLECTION : List Reviewed by on Rating: 5

3 comments:

  1. Very cool beginner tuts

    ReplyDelete
  2. Anonymous6/09/2020

    List is unordered collections of elements.

    ReplyDelete
    Replies
    1. List is ordered collection in salesforce and Set is unordered collection in salesforce .

      For reference you an check the the sandard salesforce documentation :
      https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm#:~:text=Lists,and%20built%2Din%20Apex%20types.&text=The%20index%20position%20of%20the,a%20list%20is%20always%200.

      Delete

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.