Activity Forums Salesforce® Discussions What is the difference between set() and add() method in collection of Salesforce?

  • What is the difference between set() and add() method in collection of Salesforce?

    Posted by Aditya on January 24, 2020 at 2:02 pm

    What is the difference between set() and add() method in collection of Salesforce?

    Shubham replied 4 years, 11 months ago 3 Members · 2 Replies
  • 2 Replies
  • Kirandeep

    Member
    January 24, 2020 at 3:19 pm

    Hi Aditya,

    Add method is used to insert the element and Set method is used to update the Values .

  • Shubham

    Member
    January 24, 2020 at 4:39 pm

    Hi Aditya,

    add() method adds your specified element to the list at the specified index, the element which previously existed at that particular index and all the following elements will shift 1 index , and the previous element still exists.

    Whereas in set() method the element is replaced by the element we want to insert at that particular index.

    ex:-

    a list - [1,2,3,4,5,6]

    on add(0, 9)

    the list will be [9,1,2,3,4,5,6]

    on set(0,9)

    the list will be

    [9,2,3,4,5,6]

    hope it helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos