Salesforce Apex Collections:SET & Set Methods with Examples


Set is a unordered collection of simillar datatype elements. Set doesn't allow duplicate elemets. Set will not maintain insertion order. Salesforce has predefined Apex class called Set .This class contains methods required to support the operations on the set .

Syntax :

Set<DataType> setName =new Set<DataType>();
            or
Set<DataType> setName=new Set<DataType>(List/Set) ;

Examples :
Set<Integer> ages=new Set<Integer>();
Set<String> names=new Set<String>();

Methods : 
add(ele):  This method is used to add new elements into the set .
Example :
Set<Integer> ages=new Set<Integer>();
ages.add(90);
ages.add(30);
ages.add(10);
ages.add(90);
System.debug(ages); //  {10,30,90 }

addAll(set/list) : This method is used to add list /set of elements to a set .

Example:
List<Integer> values =new List<Integer>{10,90,30,40};
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(80);
ages.addAll(values); // All the elements in the values are added to ages.
System.debug(ages);  // { 10,30,40,80,90}


remove(ele) : This method is used to remove given element from the set.

Example :
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(20);
ages.add(30);
System.debug(ages); // {10,20,30 }
ages.remove(20);
System.debug(ages); // {10,30 }

removeAll(list/set) : This method is used to remove list or set of elements from the set.

List<Integer> values=new List<Integer>{10,90,40};
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
System.debug(ages); // {10,30,50,90 }
ages.removeAll(values); // All the elements which are in values ,remove them from the set ages.
System.debug(ages); // { 30,50}

clear(): This method will remove all the elements from the set .
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
System.debug(ages); // { 10,30,90}
ages.clear();
System.debug(ages); {}


size()  : This will return  count of no of elements in the set .

Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
Integer count= ages.size();
System.debug(count) ; // 4

contains(ele) : This method will return true ,if the set contains the given element.

Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
System.debug(ages); // {10, 30,50,90}

Boolean flag1 =ages.contains(10);
System.debug(flag1); // true
Boolean flag2 =ages.contains(80); // false
System.debug(flag2); // false

containsAll(set/list) : This method will return if all the elements in the set/list are available


List<Integer> values=new List<Integer>{10,40,90};
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
System.debug(ages); // {10,30,50,90 }
Boolean flag1= ages.containsAll(values);
System.debug(flag1);// false

Set<Integer> myData=new Set<Integer>{10,30};
Boolean flag2=ages.containsAll(myData);
System.debug(flag2); // true

retainAll(set/list): This will retain the common elements in the set  and given list/set.

List<Integer> values=new List<Integer>{10,40,90};
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
System.debug(ages); // {10,30,50,90 }
ages.retainAll(values);
System.debug(ages); // {10,90}

isEmpty() : This will return true if the set is empty .
Set<Integer> ages=new Set<Integer>();
System.debug(ages.isEmpty()); // true
Set<Integer> values ;
values.isEmpty(); // error  null pointer exception

List & List Methods with Examples

Post a Comment