Activity Forums Salesforce® Discussions What is the use of retainAll(ele) in Salesforce?

  • Avnish Yadav

    Member
    September 10, 2018 at 6:58 am

    Hello Prachi,
    retainAll(): Retains only the elements in this set that are contained in the specified list.
    Syntax:

    Set<integer> set1 = new Set<integer>{1, 2, 3};
    List<integer> list1 = new List<integer>{1, 2, 4, 3};
    Boolean result = set1.retainAll(list1);
    System.assertEquals(true, result);

    Hope this will help.
    Thanks

  • Parul

    Member
    September 10, 2018 at 12:46 pm

    Hi
    This will keep only the value that is existing in the list or set.

    list<string>Names= new list<string>{'Dell','IBM','Apple'};
    set<string>Orglist= new set<string>();
    Orglist.add('Dell');
    Orglist.add('Sony');
    Orglist.add('Acer');
    Orglist.add('HP');
    boolean result;
    result=Orglist.retainall(names);
    system.debug(result);//True

    Thanks.

  • shariq

    Member
    September 15, 2018 at 10:34 am

    Hi,
    retainAll(): Retains only the elements in this set that are contained in the specified list.
    Syntax:

     Set<integer> mySet = new Set<integer>{1, 2, 3};
    List<integer> myList = new List<integer>{1, 2, 4, 3};
    Boolean result = mySet.retainAll(myList);
    System.assertEquals(true, result);

    Hope this helps.

  • manu

    Member
    January 29, 2021 at 5:29 pm

    In simple words, it compares a set with another set/list and check for common elements. if there is any common element between them, it keeps the element and removes the uncommon element.

  • Aditya

    Member
    February 2, 2021 at 6:52 am

    In simple words, it compares a set with another set/list and check for common elements. if there is any common element between them, it keeps the element and removes the uncommon element.

Log In to reply.

Popular Salesforce Blogs