Activity › Forums › Salesforce® Discussions › What is the use of retainAll(ele) in Salesforce?
Tagged: assertEquals(), Data Elements, List, retainAll, SET
-
What is the use of retainAll(ele) in Salesforce?
Posted by Prachi on July 20, 2018 at 10:30 AMWhat is the use of retainAll(ele) in Salesforce?
Aditya replied 5 years, 4 months ago 6 Members · 5 Replies -
5 Replies
-
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 - [adinserter block='9']
-
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);//TrueThanks.
-
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.
-
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.
-
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.