 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to discuss about map in Apex. So first let's discuss the theoretical aspects of map and then I will be demonstrating you with the methods that are available with map in Apex. So here you can see, as first point says, a map is a collection of key value pair. So in my previous video, I demonstrated you about list and set. So in list and set, we can store only values whereas in map, we can store two things. One is key and another one is value. So this is a pair. So together they form a single entity. Then second point says keys are always unique having a value associated. So keys must be unique and the value which we are going to store with respect to key that value can be duplicate but keys are always unique. Then values can be duplicate. Apex uses a hash structure for all maps. Then adding a map entry with an existing key overrides the existing entry with new. So for example, in your map, if you have, let's say, if you have key as one and value as test. Now for key one, you want to assign any other value. So the value test will be overridden with the new value. So that you need to remember. Then map key and values can contain any collection and can contain nested collection as well. Then keys and values can be of any data type. It can be of primitive collection. In collection, we can have lists set and map. Then S objects, then user defined types or built in types. Right. So for these data types, you can create key value pairs to form a map. Now here you can see different examples like how we can create maps. So here, first example says keys will be string and values will be string. Then in second keys will be in teaser and values will be of string type. Then in third example keys will be ID. Those are related to account and values will be account record. Then in fourth, you can see first we are fetching some account records in form of a list and then we are assigning that list to the constructor of the map and then that list will automatically convert into map and map will be having key as ID and values as account record. And last one you can see here keys are account records and values are list of context. So this way we can we can use list as values as well. Right. So now these are the methods that are available with map. So now I'm going to demo these things. So I'm going to create new apex loss map demo. And here I'm going to create a method. So map. And inside this method, I'm going to create a map and then I will be using the methods are provided through map. So I'm creating a map of in teaser or my string. So in teaser to string map. This is the name of this map and then new map of in teaser string. So this way you can create a map. Now we have a method that is put. So you can copy this name. Then you can write put. So first you need to put key and then value as string. So let's say one and a. Now I'm copying this then to hand be now I'm going to write system dot the book name of this map and plus and then map variable. Right. So if if I use map name in single code so this will be printed as it is and then actual map values will be displayed. So this I also demoed you in case of list and set. So now I'm going to open execute anonymous window from where we can execute this method. So name of classes map demo. So I'm going to create its instance. Now through this instance m dot we can call this method. Now I'm going to execute this and I'm going to open the log so that I can see that. So I can see the output. So this is incorrectly done. That's why it is showing error. So whenever there is an error so you can see that exclamation mark was there. So it executed. Now we need to click on debug only so here you can see this is the output of the map. So he is one and values a then keys to and value is B. Right. So this way you can put the values. Now here you can see a second method is put all. So if you want to put value of a map into another so you can use this method. Then if you want to put S object area so you can use this as well. Now, let me show you one more thing that I explained theoretically like if he is same and we are assigning any other value. Right. So he is to and value is C. So B will be replaced and value will be C. Right. So I'm just writing it over here. Copying this pasting this. Okay, I'm removing this. Right. So earlier, he one is having a and two is having been then this map will be displayed. Then one is sorry, I am just commenting this. I'm just modifying the key to with value C. And then I'm again printing the map. Right. So let's see what happens. I'm executing this. So two outputs will be there. So here you can see first output is showing a one key and value a to as key and B as value. Then later on you can see key to is modified with value C. Right. So the value is overwritten because on same key we are assigning different value. Right. So this way you need to you need to take this care like if key is already available and for same key you are assigning value. So that value will be over. Then if you want to remove a particular key so that is also possible. So let me add one more. Let's say three. We're moving this. We're moving this as well. And it is C. Right. So now our map is having three values. Right. Now I'm using map. Remove. And I'm passing. He has to. And I'm going to display this map again. So two outputs will be there one from line number seven and one from line number nine. So let's see what happens. So again I'm clicking on debug only. So here you can see first output is showing three values ABC and keys are 123. Then we removed key to so values also removed and only two key value pair are available. So this way you can remove a particular key with this method removed. Then if you want to know the size, you can. So I am commenting these two statements. And here I'm putting science. So you can just add dot size with the map name. So you can store it into integer variable or you can directly print it with some system dot debug. So here you can see sizes three because three key value pairs are available in the map. So this way you can see now next an important method is values. So if you want to fetch only values like ABC. So for that you can use this method. So there are two methods. One is values and another will be. Key set right so through true values we can we can separately have the values. Those are available in the map and through key set we can separately have the keys. Those are available in the map. So now this. This statement system dot debug will be printing only values. Those are available in the map. So I'm going to execute this. And also you can assign these values into some list and then you can also use that. So you can see only ABCR displayed here. Right. So I'm copying this. And let's say I'm creating list of string. Here is equals to this. So this way you can see. I am assigning map values into list of string because here you can see a value data type is string. So that's why I created list of string. And then I'm going to display this. So I'm executing this. So this way you can assign values to a particular data type. So here you can see output is same. Right. So whatever type is available here similar type of list you can create and you can assign the values with the help of this values method. So this way you can you can fetch all the values. Those are available in the map. Then next method is so let me first explain this method key set because we are discussing this. With values. So I'm commenting this. Right. Or here directly I'm going to use this dot. Right. So now this will be printing only keys as output. The keys basically you can store into set. If you want to store because keys are unique. So here you can see these are the keys one to three. Right. So now you can create a set integer because keys are integer. So it's set equals to and this you can write here. So this way all the keys will be stored. Here something is incorrect. I forgot to write new. That's why it is showing the error. So now it is correct. I'm executing it again. So this time all the keys will be stored into set and then I'm going to display the set. So here you can see keys are displayed. So this way keys you can store into set because those are unique and values you can store into list because they are there. So values can contain duplicates as well. And in list you can store duplicates. So this way you can divide your map into certain list as well. And similarly if you have set unless so you can combine them as well. Right. So he said we saw how we can use then we have clear. So if you want to clear the values of map, you can do that as well. So I am removing all these and here I'm putting this again. And here I'm using clear. So what will happen line number seven will print all the values with all the key and value pair which are available into map. Then line number eight will clear all the data which is available into map and then line number nine will display blank map. So I'm going to execute this so that we can see the output. So here you can see first map is sorry first system dot debug is displaying map values, then we called clear method so map is clean and it is displaying nothing. So this way you can erase the values of map. And then the next method is clone. So for cloning you need to create one more map. So I'm copying this thing one here. So this way you can assign or you can clone a particular map into another and here I'm putting this thing. So both will be displaying same values. Right. So I'm going to execute this. So here you can see both are displaying same values. One is from into SDR map. And another is from into SDR map one. So this way you can clone the values as well. The next important method is contains key. So it basically checks whether a particular key is available in map or not. So it returns true or false. So we can see contains key to. So if it is available, then it will be displaying true otherwise false. So we can use this into if condition as well. So you can put that part into it. So here you can see it is showing true because key to is available. So you can use this part in if condition as well. So if it is written in true, then some set of instruction will execute. Otherwise other set of instruction will execute. Then equals will compare two maps. Then through get we can have the value corresponding to a particular key. So this is also important. So here I'm using get. So what will happen? I'm getting value against key number two. So in output, it will display only B because for key to a value is associated as B. So here you can see. So this way if you want to get a particular key keys value, then you can use this get method. And is empty will check whether the map is empty or not. So you can apply this as well. Is empty. So sometimes whenever we query records from the Salesforce object and nothing is queried. So before iterating that map, we need to check whether it is empty or not. So for that purpose, we can use this is empty method with checks whether map is having something or not. So here you can see it is showing false because map is not empty. If it is empty, then it will display true. So this way I demonstrated you all the methods. Right. So you can create these things as well. So let me demo you one of the object related thing. So I'm cleaning all these. So let me create a map of ID account. So a CC ID, sorry, account ID map. Right. Equals to new map of ID, account. Right. So in PPT, you can see when we are creating new maps. So here I'm passing this account list. So here what I can do, I can write select ID name from account. And then system.debug. So here I'm putting this plus and then again this I'm saving this. So I'm going to fetch two records from account object. So it will return a list because whenever we query, we apply SOQL. So automatically list is returned. So this list will pass into the constructor of map and that will convert into ID and account record automatically. So now I'm going to execute this. So you will see the output in form of ID and account record. So here you can see account ID map. So this is the ID and then account record details. So account record detail is having only ID and name because we added only two fields. So this is the first record and this is the second record. So with ID account details are associated. So this way you can use the map. So normally we don't query like this. So maybe you have your data already available into list. So then you can iterate that list and you can assign data into map or you can directly pass those lists into the constructor of the map while creating the map. So this way you can use your map with S objects as well. Right. So I hope you understood how we can use these methods of map or performing various tasks. And with this explanation, you understood what map is. So I also created videos on list and set separately. So those are available in this playlist. So you can watch them as well. So these are three collections list set and map. So all three we need to use whenever we work with apex. So you need to understand all these things properly. So I tried to give demonstration related to list set and map in three individual videos. So watch all of them all of them so that you can understand the concepts and later on in this playlist, you can watch other videos as well related to apex. And on my website study Salesforce.com, you will find all the links related to Salesforce videos as well. So do visit that website so that you can have all video links together on single platform. So I hope you understood whatever I demonstrated regarding map and apex. Thank you for watching this video.