Different Kind Of Maps *** Interview Question ***

Complete Step By Step Java for Testers Java Advanced - Collections Framework
10 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€85.87
List Price:  €120.23
You save:  €34.35
£74.39
List Price:  £104.15
You save:  £29.76
CA$138.18
List Price:  CA$193.46
You save:  CA$55.28
A$153.39
List Price:  A$214.75
You save:  A$61.36
S$128.92
List Price:  S$180.49
You save:  S$51.57
HK$779.80
List Price:  HK$1,091.76
You save:  HK$311.95
CHF 80.49
List Price:  CHF 112.70
You save:  CHF 32.20
NOK kr1,009.67
List Price:  NOK kr1,413.58
You save:  NOK kr403.91
DKK kr641.01
List Price:  DKK kr897.45
You save:  DKK kr256.43
NZ$170.65
List Price:  NZ$238.92
You save:  NZ$68.26
د.إ367.21
List Price:  د.إ514.11
You save:  د.إ146.90
৳12,159.63
List Price:  ৳17,023.97
You save:  ৳4,864.33
₹8,813.74
List Price:  ₹12,339.59
You save:  ₹3,525.84
RM422.70
List Price:  RM591.80
You save:  RM169.10
₦152,684.73
List Price:  ₦213,764.73
You save:  ₦61,080
₨28,172.18
List Price:  ₨39,442.18
You save:  ₨11,270
฿3,233.20
List Price:  ฿4,526.62
You save:  ฿1,293.41
₺4,116.73
List Price:  ₺5,763.58
You save:  ₺1,646.85
B$546.03
List Price:  B$764.47
You save:  B$218.43
R1,779.46
List Price:  R2,491.32
You save:  R711.85
Лв167.77
List Price:  Лв234.89
You save:  Лв67.11
₩139,375.48
List Price:  ₩195,131.24
You save:  ₩55,755.76
₪336.41
List Price:  ₪470.99
You save:  ₪134.57
₱5,711.89
List Price:  ₱7,996.87
You save:  ₱2,284.98
¥14,830.67
List Price:  ¥20,763.54
You save:  ¥5,932.86
MX$1,876.23
List Price:  MX$2,626.81
You save:  MX$750.57
QR364.02
List Price:  QR509.64
You save:  QR145.62
P1,342.46
List Price:  P1,879.50
You save:  P537.04
KSh12,918.70
List Price:  KSh18,086.70
You save:  KSh5,168
E£4,854.20
List Price:  E£6,796.08
You save:  E£1,941.87
ብር14,292.55
List Price:  ብር20,010.15
You save:  ብር5,717.59
Kz91,186.39
List Price:  Kz127,664.59
You save:  Kz36,478.20
CLP$96,831.31
List Price:  CLP$135,567.71
You save:  CLP$38,736.40
CN¥713
List Price:  CN¥998.24
You save:  CN¥285.23
RD$6,296.92
List Price:  RD$8,815.94
You save:  RD$2,519.02
DA12,990.59
List Price:  DA18,187.34
You save:  DA5,196.75
FJ$226.12
List Price:  FJ$316.58
You save:  FJ$90.46
Q765.93
List Price:  Q1,072.34
You save:  Q306.40
GY$20,890.84
List Price:  GY$29,248.01
You save:  GY$8,357.17
ISK kr12,333.76
List Price:  ISK kr17,267.76
You save:  ISK kr4,934
DH908.31
List Price:  DH1,271.67
You save:  DH363.36
L1,670.58
List Price:  L2,338.87
You save:  L668.29
ден5,277.34
List Price:  ден7,388.49
You save:  ден2,111.14
MOP$802.43
List Price:  MOP$1,123.43
You save:  MOP$321
N$1,761.48
List Price:  N$2,466.14
You save:  N$704.66
C$3,674.73
List Price:  C$5,144.77
You save:  C$1,470.03
रु14,082.30
List Price:  रु19,715.79
You save:  रु5,633.48
S/353.21
List Price:  S/494.51
You save:  S/141.30
K416.61
List Price:  K583.27
You save:  K166.66
SAR375.17
List Price:  SAR525.25
You save:  SAR150.08
ZK2,374.72
List Price:  ZK3,324.71
You save:  ZK949.98
L436.04
List Price:  L610.48
You save:  L174.43
Kč2,099.65
List Price:  Kč2,939.59
You save:  Kč839.94
Ft33,821.21
List Price:  Ft47,351.04
You save:  Ft13,529.83
SEK kr945.93
List Price:  SEK kr1,324.34
You save:  SEK kr378.41
ARS$136,092.68
List Price:  ARS$190,535.20
You save:  ARS$54,442.52
Bs690.01
List Price:  Bs966.04
You save:  Bs276.03
COP$400,414.31
List Price:  COP$560,596.05
You save:  COP$160,181.74
₡50,481.69
List Price:  ₡70,676.39
You save:  ₡20,194.69
L2,617.73
List Price:  L3,664.93
You save:  L1,047.20
₲721,217.97
List Price:  ₲1,009,734.02
You save:  ₲288,516.04
$U4,006.62
List Price:  $U5,609.43
You save:  $U1,602.81
zł365.33
List Price:  zł511.47
You save:  zł146.14
Already have an account? Log In

Transcript

Hello guys, welcome back to the next tutorial. So in the last one we discussed about the maps. And in this one I'm going to talk about the different kind of maps and how we can iterate over the maps. Since we have maps, we definitely need to know how we can iterate over those different entries and the keys and the values right, in order to retrieve stuff and perform actions from them. So what I've done is it's a new class map situation. And I wanted to define the map, which is the basic one that we talked about in the last class.

This is the hash map. This is the command that it does not maintain any honor, as you have seen in the hash set. And I've just entered all those three values that we define entries, I would say the values, three entries that we defined in the last tutorial. So let's see what others how we can iterate over these. So as we know that these are entries, the way we iterate over these is we're going to use the entry set, right? So map dot entry.

This is the this is the entry and it's going to have key value, right the key represents key and the way we represent value. So the key is of type integer right now, and the value is of type string. And there's a typo, right. So this is our key. And this is our value. And we're going to provide a name here.

So let's say this is our entry out of our complete entry set. So since one, one key value pair is an entry, and all of these, we can say this is a set of entries that we have. So to trade our one entry, we're just giving it a name. So this is a name dice, you can just say item as well, you can just say, values on anything. So what I'm doing right now is empty, it will be just more obvious. So entry, or our entry set, right?

So the name of our map is map here. So maybe let's just say, hash map, something like that. So it's more obvious what kind of map you're working on. So let's just copy these two here, and here and here. And here. We can just say, hash map dot entry set.

So this is our complete entry server, it's going to have all the entries. And we're going to iterate over these entries one by one. So this is our for loop. And here's how we iterate over the map. So what we can do right now is we can find the keys. So since the keys of type integer, we can just say and and just define a variable called key.

And from the map, or we can do is, we have the entry, we're iterating over the entries, right entry dot get key, it's just going to give me the key of that particular entry. And then I can define string value, and then mp3 dot get value, it's just gonna give me the value of that particular entry. And don't worry about using the end instead of integer. Java is gonna take care of the auto boxing so it doesn't even matter. It's just gonna take care of it. So right now what we're doing is we can just I can just print this thing out and show it to you.

So let's see. Say key. And I can use the key here, class. Let's format it properly value is our value, right? So I can just run the score and show it to you. So key one value BMW key four value Honda key six, value Adi.

And you guys can see this no particular order, right? It's right now looks like natural order one, four and six. But you cannot rely on that because it's a hashmap and does not maintain any order. So we enter in the order of BMW, Audi, Honda, but we are getting a different order which is, which is more looking like a natural order. But I cannot allow that because that's what the definition of the hashmap is. So that's what the hashmap looks like.

So let's take a look at the other and the other way of iterating or the maps. So the other ways what we can do is since we have the keys and the values, right, we have all these entries. So the way it maintains the entry set and reset the hashmap in maintains a key set as well. So what we can do is we can just simply iterate over the key set. So since our key is of type integer, we can just say this. And iterating over the key set lets you use the key.

And this is just the name again, one key for the key set. So we're going to iterate over each key, right? And hash map, right, this is the name of our map, dot key set. So similar way, we had an entry set, now we have a key set as well. And we can just go straight over this. So how we can relate the retrieve the value now we can just see the values of type string, so string, and maybe value.

And then we can just see our hash map dot get. And since we have all the keys, we had a Trojan what were the keys we can just use this key, right? This is how we get values right? So we can just use get Key, now we're going to have the value. And I can just see see. So right here, what I can just copy this, I can straight away copy this because it's going to be exactly the same.

So I can just have one more SQL statement here, too. Creation Tool, right? So we know that which iteration we are looking at. And this is equation number one. So let's run this code again. Situation one is this, and attrition to business.

So we pretty much can achieve the same thing using either of the ways of trading or the maps. And what I was talking about is this thing that you guys might see that I'm using the same variable names by the string values is string values here, but don't worry because they are inside the for loop. So they have the scope scope of the for loop, only. This one is not visible outside the follow. So I can again use it in a different block. So this is a completely one block only one block of code, so it's not visible outside.

So inside two different for loops, we can use the same name. But if I have a string value defined here, let's say this, I'm going to have no space because this value name is defined outside of follow, which is visible in the complete method. So I cannot define a value here, right? So this is pretty okay. So now let's talk about the different kinds of maps, right? So similar way we have maps, in the same way that we have the hash map.

So let's just copy this path from here. And let's just have it here. So instead of a hash map, what we can have is a linked hash map. This is one type. And let's call it link, right link map, something like that. Just to make it shot and imported imported from the Java dot util.

And exactly in the same manner. We can have a key map. And they're exactly like the way we talked about inset, right? So, exactly the same way that link hashmaps. They maintain the order in which they are added. Right.

And the tree map maintains natural sorting order. Natural sorting, right. So that's what the thing is. And what I can show you is maybe just quickly have the things here, copy this here. And instead of hashmap, let's put link map here, right. So let's put the link map here.

And let's quickly trade over this one. I just want to copy this. So let's quickly iterate over this one. And so the hash map, I just want to say the link map because that's the one I want to iterate over. And let's call it iteration number three. So in this one, what we expect is because it's a link map, the order should be maintained.

So the order should show as BMW, Audi, and Honda because that's what it says. It maintains the order in which they are added. So let's just run this code. And let's see the output. So for iteration one, and two, you guys can see this, right. But for the iteration three, you can see the key one, and then it appears the key six and then base the key four, so it is maintaining the order in which they were adding, and that's the benefit of being hashmap.

Now, comes the question that what's the difference between hash maps Ling hash maps and the tree maps. So that's what the difference is the order of integration. So hashmap makes no guarantee Which is kinda a trade, it can just be trade in any arbitrary order. But the three maps are always going to maintain the natural sorting and the length hashmaps are always going to maintain the order in which the entries were added. This is the difference. So another thing is that the hash maps, they allow one null key.

So we can add one null key in the hash map. Because the case cannot be duplicate. Obviously, there cannot be more than one key because otherwise it's gonna be duplicated. So other thing like when do we want to decide which one to use either a hash map or the tree map. So it is pretty simple. If you are just using it for a plain simple purpose always use hash map because that's faster.

It does not have any button on it. It does not have to maintain any order. It's just quick. But if you want that whichever order you are inserting values you want to retain that use link linked hash map. And if you want to maintain the order of natural sorting, which comes with The burden because then the tree map has to sort them in the natural sorry, then use the tree map, which comes with a little bit of performance hit and are not very stable, you're not going to see the performance in the small map. It's always seen when you have a large quantity of entries added.

So just make a wise decision in which whichever map you want to use, what's your use case? So for the simplest one, always go with the hash map. So yeah, they just try to see these guys. Try to see how you can iterate over the maps and use these different kind of maps. So that's pretty much it for this one, guys. Thanks for attending this one.

Stay tuned for the next one, guys. Thanks, guys.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.