What is the use of map implementation in the Collection framework?

What we will do we will uh check like we will start doing the map implementation right so we will call map I want a key type of string I am going to add that that is string I want to have a value that is a type of integer right I am going to write integer so one thing you should remember it does not take in the short form it of the short form of it right you need to write the full form that what type you want or string for string it does not take ESTIA you need to write stringing right the full form for integer you need to write the full form that’s an integer then you need to provide the name right equals to I am going to call new what type of uh map I want to call hash map right.

Let me check right here again I am going to repeat what type of key I am going to use that is a string and the value type is integer right so yeah this is how we can initialize map now we are going to add some value to it okay so here I use yeah I use table map dot here we what we do we use put function right to add something we use put function to add something to our map right first I’m going to add some name and according to that name I am going to store the value right so let’s see I’m going to add a meal and then the value I am going to add maybe he got something around 50. I mean he’s not that good a student.

You can see this is how we can add the data on the map right so again I’m going to add some more data to see other methods we have right why it’s setting to three more value to show you guys how actually it’s working all right so maybe john he is also a good student and this is how you need to explain our you need to uh add your data to the map right it actually takes your data in a key-value pair for that if you want to extract data using the key you can do that if you want to extract data just the values.

There’s a key you will do that right so it’s kind of a pointer kind of thing you can say like using key you can extract the value right let me uh just print system dot out dot green tailing I think you guys are quite clear why we use print and queen talent, yeah you can see we got it like for the john I got 90 for ya we got 76 for SAM we got 80 and for and we so one more thing you can see it does not maintain the insertion order right as I say hash map does actually maintain the insertion order let’s see if I got going to use our tree map maybe it’s going to uh maintain the insertion order right but no but map also does not maintain the insertion order only linked hash map maintain the inflation order so let me add something over here for linked hash map again the same thing I’m going to do and here I am like in session order is going to maintain or not.

Let’s see right and I’m going to put the same value over here let’s see how it’s going to put right so we will get the difference between them so when uh for the interview time it we actually asked what’s the difference between hash map what is the difference between link hash map why in what scenario you need to use those things be sure guys you have the proper idea that what is hash map what is the difference between hash map and linked hash map and tree hash map right so I think this will guy help you out let’s see what we will get at the output, yeah you can see I actually gave Anil first then SAM then john India yeah it’s maintaining the insertion order the water we have right let’s see if we go for the tree hash map if it’s going to give us a sorted value or not right.

We will go to uh use a tree hash map let’s see it’s going to give us a sorted order in sorted order or not our output is going to sort it or not let’s see so yeah what we need to do we need to call map then string what type of key you want then the value what type like your value type then the name you want to give I give a table map to for tree map and then comes new then call that your tree map interface and then this is how you can call it right now I’m going to add this same value for the uh tree map as well let’s see what in output we will uh get from their right so I just need to change the name for them.

Here we go let me just execute the code,, yeah you can see that the first one we have that is a hash map it does not maintain any insertion or order or not even in sorted order for a link hash map it does maintain the insertion order we have but it does not give you the sorted order, but tree hash map will give you the sorted order output right if you want to have a sorted ascending order or sorted order output you can use free hash map, so this is the basic difference between three types of the map you guys should uh remember all these things that actually in the interview they asked all these things right.

Now let’s see what other things we can do like suppose maybe you want to interact like in uh printing that map you will get all the value but if you want to have an only key if you want to just want to page the key or just want to face the value how can you do that in that case what we use we use iterator as well we used to follow so here I am going to use for loop and I will show you how you can do that right so first, we need to write for then under that we need to write map right then what we need to write dot entry okay I’m going to add entry method and here you need to put the k and v right.

If you want to iterate through every element of your map how can you do that or if you want to have just the values if you want to have just the keys of your map how can you do that you can use an iterator or you can use follow so here I’m going to use follow so what I need to do I just need to write for the for loop and then I just need to call the map dot entry method I need to call then I need to write map and then semicolon your map name that here I am using demo map right and then you need to call dot entry set okay, so this is the uh like signature you need to understand you need to remind iterating through every element in the map right now I am going to write system dot out dot print Ln right now what I need to do I just need to call my uh value on the key.

Leave a Comment