Set methods in Salesforce with interesting examples





Set
  • A set is an unordered collection of elements. 
  • Set elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types
  • Set does not allow duplicate value
  • Insertion order is not preserved in the set.
  • You cannot perform DML with Set.
Syntax: 

Set<datatype> variablename = new Set<datatype>();

Example:
 

  Set<String> bollywoodActresses= new Set<String{'Deepika','Anushka','Alia'};
  
Hence values are stored as,
      index postion 0 = Deepika
      index postion 1 = Anushka
      index position 2 = Alia


Methods in Set
There are few methods available in Set
add(set Element)
add(index, setElement)
addAll(from List)
addAll(SetElement)
clear()
clone()
remove(setElement)
size()
equals(set2)
hashCode()
isEmpty() contains(element)
containsAll(setToCompare)
retainAll(listOfElementsToRetain)
removeAll(setOfElementsToRemove)

 add(setElement)
 It inserts an element into the set. 


        Set<string> bollywoodActresses= new Set<string>();
        name.add('Anushka');
        name.add('Deepika');


addAll(fromList)

Adds all of the elements in the specified list to the set if they are not already present.
   Set<string> bollywoodActress = new Set<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       System.debug(bollywoodActress);
    List<string> bollywoodActor = new List<string>();
       bollywoodActor.add('Salman');
       bollywoodActor.add('Ranveer');
       bollywoodActor.add('Ranbir');
       System.debug(bollywoodActor);
       bollywoodActor.addAll(bollywoodActress);
       system.debug('Both list names are '+' '+bollywoodActor+'');

addAll(fromSet)
Adds all of the elements in the specified set to the set that calls the method if they are not already present.

Set<string> bollywoodActress = new Set<string> {'Deepika', 'Kareena','Alia'};
Set<string> bollywoodActor = new Set<string> {'Salman', 'Ranveer', 'Ranbir'};
        bollywoodActress.addAll(bollywoodActor);
        system.debug(bollywoodActress);
   

size()

It is used to get the total number of elements in the set.



 Example: bollywoodActor.size();



remove(setElement)
Removes the specified element from the set if it is present.


   Set<string> bollywoodActress = new Set<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
      System.debug(bollywoodActress);
       bollywoodActress.remove(''Deepika''); //remove 2nd position value.
      System.debug(bollywoodActress);



clear()

It will remove all the elements from the set. 



  Set<string> bollywoodActress = new Set<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       System.debug(bollywoodActress);
       bollywoodActresses.clear();
       System.debug(bollywoodActress);



clone() 
It creates another copy of the set.

  Set<string> bollywoodActress = new Set<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       System.debug(bollywoodActress);
  Set<string> secondSet = new Set<string>();
      secondSet =bollywoodActress.clone();
      System.debug(secondSet );

equals(set2)
Compares the set with the specific set and returns true if both are equals, otherwise, returns false.

 Set<string> bollywoodActress = new Set<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       System.debug(bollywoodActress);
      
 Set<string> bollywoodActor = new Set<string>();
       bollywoodActor.add('Salman');
       bollywoodActor.add('Ranveer');
       bollywoodActor.add('Ranbir');
       System.debug(bollywoodActor);
       Boolean result;
       result=bollywoodActor.equals(bollywoodActress);
       system.debug(result);

hashCode()
Returns the hash code corresponding to this set and its contents. 


 Set<string> bollywoodActor = new Set<string>();
       bollywoodActor.add('Salman');
       bollywoodActor.add('Ranveer');
       bollywoodActor.add('Ranbir');
       System.debug(bollywoodActor);
       System.debug(bollywoodActor.hashCode());
//Output: 485459500


isEmpty()
True if the list has zero elements.

Set<string> bollywoodActor = new Set<string>();
       bollywoodActor.add('Salman');
       bollywoodActor.add('Ranveer');
       bollywoodActor.add('Ranbir');
     System.debug(bollywoodActor);
  System.debug(bollywoodActor.isEmpty());
//Output: false

Contains (Element)
Returns true if the set contains the specified element.

 Set<string> bollywoodActress = new Set<string>();
       bollywoodActress.add('Deepika');
       bollywoodActress.add('Kareena');
       bollywoodActress.add('Alia');
       Boolean result =bollywoodActress.contains('Deepika');
       System.debug(result);


containsAll(setToCompare)
Returns true if the set contains all of the elements in the specified set.
Set<String>myString = new Set<String>{'a', 'b'};
Set<String>rString = new Set<String>{'a', 'b', 'c'};
   Boolean result2 = myString.containsAll(rString);
   System.debug(result2);// False

Share this

Related Posts

Previous
Next Post »