 Hello friends, I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to discuss and demonstrate how we can use set in Apex. So in my previous video, I demonstrated you the use of list. And in this video, first we will discuss about the set theoretically and then I will demo you some methods that we can use with set. So set is basically a collection similar to list, but it is an unordered collection of elements. It doesn't contain duplicate elements. Set can contain collections that can be nested with one another. And elements can be of any data type like primitive collection as object, user defined type and built in types. So in list, we can store duplicates as well, but in case of set, we cannot. So normally, whenever we want to store IDs of records, then we prefer set instead of list. And to store all information, we prefer list because whenever we apply any query, so the returned result will be in form of list. Right. So these are some examples how we can create set. So you need to write set then in Angular bracket, you can write the data type. So here is string ID and integer three types are mentioned. And these are some methods that we can use to create set or to use set. So I'm going to create new Apex class. Set demo, public void set method. So now I'm going to create a set of string str set equals to new set of string, right? So similar to list we can create set as well. And if we see the first method, so first method is add a set element. So what we can do str set dot add and this way we can add the element. And if you want to display system dot debug, then str set str set. So in single quotes, whatever we write will be displayed as it is. And this is the variable that we created and its value will be displayed. So I'm saving this. Now I'm going to use this execute anonymous window. So this I opened through the shortcut control plus E. So set demo as equals to new set demo and as start set method. And clicking on this open look so executing so debug log will be opened automatically so that we can view the output that will be displayed through system dot debug. So here you can see str set equals to India is just right. And if you want to add one more element. So I'm adding same values, and I'm going to execute this. So you can see I added India twice but only one entry is available because set doesn't contain duplicates. And whereas if we use list. So here also I can initialize it. So this way I created one list, having both same values and set having same values. Now I'm going to execute these both. So you will see set won't be containing duplicate but list can contain duplicates. So this is the major difference that you need to know. Now we have two methods at all. One will add all the elements of list to set and then from set to set. So first I'm going to add all with list so str set dot add all and here I'm passing str list. So now I'm removing this. So our set is containing one value as India. Then I'm adding list into set but list is containing two same values. So let's see what happens now. So set will be containing only two values India and a and str list is containing a comma. So as I told you, set is an unordered collection of elements, right? So you can, you can see this output. So India was already there in set and then we added a but a is available at first position and India is second position. So in list indexes are important. So as per the index values as to if list is containing value already then the remaining or newly inserted values will be added at the end. But this is not the case in set. Now next is add all from set. So I think you can do this. You can create one more set and you can add it to the set, right? So try this at your own then clear and clone. I'm going to tell you so here I'm going to use str set dot clear and using this. So here you can see our first time adding India into set then I'm adding list into set then I'm displaying set then with this clear method I'm clearing the value of set and then again displaying the set. So let's see what happens now. So first system.debug will display some values then second system.debug will not display any value because clear method is written before that. So here you can see at line number nine we have used system.debug that is printing values, but line number 11 system.debug is nothing printing because we have used clear. Now I need to use one. I need to create one more set of string str set to stop string. So this I'm going to use for clone. So through clone we can copy value of one set to another. So here you can see. So initially what we did we provided some values to set the direct value and through list then I displayed str set value then I'm cloning str set and assigning it into str set two and then displaying it. So both will be printing same values. So here you can see both are printing same values. So this way you can use this clone. Now we need to check whether a particular element is available in the set or not. So we can use contains. So we can directly use it. So I'm removing these and I'm writing str set dot contains a. So it returns value in form of true and false. So I'm directly using it in system.debug so it will be printing true or false. Also what I can do I can create one more Boolean variable then I can assign the result into Boolean and then can display that. So both fees are correct, but it depends which is required while working on the projects. So here you can see line number 10 system.debug is printing true because A is available in the set. Right now if you want to create a Boolean variable Boolean B equals to sorry Boolean B and then B equals to and I am using this and I am writing B here. So it will display value of B. Then we have one more system.debug removing these. So right now we have two system.debug at line number 11 and 12. So 11 will be displaying value of B and 12 will be displaying a result as per this contains method. Right so B is not available in the set. So first output will be false and second output will be true. So let's check it. So this way you can store the result of contains into Boolean variable and then you can use it further. So you can see B is printing false and line number 12 is printing true. Right so if you store output into a variable then you can use that variable for further processing as well or you can use this directly. It depends totally upon your requirement. Then contains also this will check list to compare and this will check set to compare. Right so you can compare a set with list and you can compare set with another set. So let's try this as well. So now what I'm going to do. I'm going to add A and then B. Right so removing this statement. So here you can see a list is containing AB. So I'm assigning A and B into list and I am aside. I am adding A and B into set as well. And then I'm writing contains all. So I'm going to check this STR CAT with STR list. So this way I used this contains all. So here you can see set is available and here I'm comparing it with list. So let's see what happens and printing result through B. So B will be containing either true or false. So here you can see it is displaying true because both list and set contains same values. Right now we have one more set. So I'm adding only A into it and I'm going to compare it with set. So now we are comparing two set STR CAT and STR CAT 2. So STR CAT is containing AB and set 2 is containing only A. So let's see what happens when we execute this. So right now we are comparing two sets. So here let me execute it again. So here you can see we are checking contains all. So STR CAT 2 is having only A and this A is available into this set. So basically what is happening, the argument which we are passing here, whatever value is available in this, whether it is set or list, contains all is checking whether all these values are available in this set or not. Right. It is not checking exact match. It is checking whether this parameter that we are passing these values are available inside set or not. Right. And for equal check, we can use this equals. Right. So if I apply equals. So now it will be comparing exact values. So this time it will display false. Because both are not exactly matching. So if I click on debug, so here you can see these displaying false. Right. So this way you can check contains all or checking whether the provided parameter values are available in a particular set or not. And equals will check whether both sets contains equal values or not. Then we have is empty. So sometimes we require this method like whenever you want to apply any process on set. So you need to check whether set is empty or not. So this way you can check strs at dot is empty. So result will be available into B or you can directly use this into system or debug or if you are applying any if condition. So in that if condition also you can use this. So let me use that as well. So I'm not using if condition if printing empty. And else, right. So if strs at dot is empty, if it this set is empty, so this condition will be true. So it will be displaying empty. Otherwise it will display not empty. So this time, our set is containing value. So it will be displaying not empty. So this way you can check if condition before applying any process to set. So that on empty set, you won't be applying any process. Then remove set elements. So this we can also apply. So strs at dot remove a. So I'm writing system debug strs at. So before removing, I'm going to display these values. And after removing, I'm going to display the values so that we can validate whether remove is working properly or not. Right. So initially a set is containing AB. So AB will be displayed. Then I'm removing a so it will the second system dot debug will be displaying beyond. So now we need to check the output. So you can see at line number nine, the system dot debug we are using it is displaying a and be both. And the 11th number line number 11 system dot debug is displaying only be because we removed a from the set. So this way you can do this, then remove all. So whatever list of element you will pass those elements will be removed. Whatever set of element you will pass those elements will be removed. Retain all. So these elements will be retained and other will be removed. So let's try them as well. So let me add some more values to this set. So I'm applying this only for. So let's apply. This remove all first. So I'll be passing list of element and those elements will be removed. So this is our set. This is our set which is containing ABCD. So I'm displaying it. Then remove all. And here I'm passing str list. Here I'm passing str list. And again, I'm printing str. So remove all method I'm going to use and the elements which will be removed are available inside str list. Right. So I'm going to save this. It is showing some. Yeah. It is incorrectly. So set is containing ABCD. That will be displayed first. Then I will be removing the elements which are available into list. So list is containing a B. So it means this str. Should be displaying C and D only. So here you can see first debug is printing both all four values ABCD and second debug is displaying only. So this way I'm passing list here. And if you have any set like str sq2, we have another set. So if we pass set, so the values which are available into set those will be removed. Now let's check retain all. So I'm applying retain all. So retain all will retain these values which are available in the list and other values will be removed. So A and B will be retained and C and D will be removed. So debug only so you can see ABCD. So C and D is removed and A and B are retained. So this way you can use all four methods then size. So size is very simple. You can simply display size of set so str. So this will display size of the set. So here you can see sizes for because it is containing four elements. So this way I demonstrated you all these methods which are popular by using set. So I hope you understood all these methods how we can use. And also remember that whenever we want to store IDs of records, then we create this kind of set. So you can create set of ID. So this way you can create set of IDs and you can store IDs of various records in this set. So this set of IDs are more popular while you work on epics with S objects. So if we have a list, then we can fetch IDs and we can store those IDs into that set so that unique collection of IDs will be there and we can use that set further. So if you are using your epics with trigger so in trigger we pass normally set of IDs and we store those IDs into set and then we can use them for further processing. Right. So this is all about set that we use in epics. So I hope you understood whatever I demonstrated. Thank you for watching this video.