Work and let's look at hash map. 2
But before going to the mall let's look at a few important things which are relevant to hash map. 3
I think we already discussed most of these points but let's do it one more time just for the sake of 4
completeness. 5
The methods get undermost brook and Constantine and that hash map insertion order is not preserved as 6
the elements are stored based on using it hash function hash map but it's not a lot of use. 7
And also one Halki. 8
Finally two methods are not synchronized unlike that legacy hash table class. 9
Now if synchronization is required it can always be taken care of externally. 10
As you'll see later when we get to the might be trading partner. 11
So that's about it. 12
And it's not time for that Demel took it for this demo a new class called map the more has been added 13
to the collections package just like bless them all. 14
I answered them all. 15
So in my of them all we have this matter hashmarks them all which we are going to fill now. 16
So in this demo they will test all the basic operations which we have seen earlier and we will also 17
look at how to iterate a map. 18
So we will be looking at the human. 19
So using the view that we can do it and initially we will create a simple hazmat later we will also 20
create a slightly more complex hash map. 21
So let's just go ahead and create a hash map. 22
That said the key is string. 23
So that's the generic notation. 24
So let me import let me just do a control of all. 25
And that automatically imported. 26
So here the brackets are empty here. 27
Just know that we can also have a string on integer specified here. 28
But it is automatically inferred by the compiler. 29
So here we are programming to an interface your map. 30
And so we have created the hash map. 31
Let's go and put some elements here 32
. 33
So here us three users John-Roger to iron. 34
Let's say these are their ages and we're on it. 35
Let's leave the value as another. 36
OK. 37
Now let's just print this 38
space map one. 39
So let me just run this. 40
It's in from the main You can search prints all of the values your key value pairs. 41
Now let's all right. 42
And it does mapping and axis and map. 43
My one word. 44
All right. 45
You let's just put some value here on the tree. 46
And 47
so as you can see the only value got over it and the new one. 48
OK. 49
Now let's try the contains ski operation. 50
So let me just say 51
map 1. 52
Lewinsky and it's just search for John 53
and it says Drew. 54
OK. 55
Now let's get a value. 56
So this is an Bensky operation that are done. 57
True. 58
Now given a key just fetched the values so that's the lookup operation. 59
That's the most commonly used operation with the hash map. 60
So let's get John's age now. 61
So we need to use get and John. 62
OK. 63
So that's the H here. 64
Now let's just go ahead and do it through the map. 65
OK. 66
So let's try hard but it don't matter. 67
There are a couple of ways to do it. 68
So first let's try to do that using the keyset method. 69
So keyset is one of the collection methods. 70
So let's get the research. 71
So let's just extract the keyset and for that we can use the keys at method. 72
So keyset what we've done is set. 73
So we call on the set of all keys. 74
So it's names you do control shift 0 9. 75
So use a for each statement. 76
I did it over the names. 77
OK. 78
Now we can say my plan got get all the name of the careers of the current name. 79
OK. 80
So let's just run it. 81
So here we are reading using keyset and we redid it in over all the names all the keys and for each 82
name we are we are fetching the corresponding h. 83
OK. 84
So that's one way off iterating on the second way as we can also do read using the entry set method 85
. 86
OK so let's also do that. 87
So the entry cert Vawdrey done a set of all mappings. 88
Let me just say. 89
And in this case it will be set off if you recall it would return an instance of the the entry interface 90
and we said it was an upstart interface. 91
So each mapping would be an instance of this particular nested interface and tree. 92
And the key value for that is we know that it's a strain. 93
Each entry is a string and the value is integer. 94
OK. 95
Just don't worry if it is a little bit confusing with the generics notations. 96
I think it will get uglier when we get to when we discuss genetics. 97
So now let's go ahead and iterate through this. 98
So here let's change your two mappings. 99
And here this would be let's just call. 100
Does not being. 101
And that type would be this would be done by the way. 102
Now for Meem we can just say my being d'arte get. 103
So this is one of the methods and entry interface. 104
Ok and for H It just see Dot get right you can not see. 105
So it's just going to print on the values. 106
So this is just an alternative way off iterating. 107
Personally I would prefer this one for iterating because you know we are fetching all of the entries 108
and all we need to do is invoke that for each entry we are invoking the get an get value had us here 109
. 110
This is also fast. 111
We are iterating through to each of the keys. 112
But then here we are doing a search. 113
Most of the time it's going to be constant don't really know but sometimes you know if a lot of these 114
get mapped to one particular bucket then it can be slightly ex-manager. 115
So probably this is a better way of doing it. 116
OK. 117
So we are done with that. 118
Now what we can do is we know that this return set is back but the original set. 119
So any changes we make here will impact the bact and the actual map also the Mac actual hash map. 120
So that sort of tells that. 121
So here let's just say names doddery more on that just right the remote. 122
Anita 123
So you run it as we can see we only see John and Raaj and the mapping corresponding and corresponding 124
to on it got deleted. 125
So that's about it that's the simple hash map lets start to create a slightly more complex hash map 126
so that we can see how flexible lead can be used. 127
So earlier it was a map of string and integer. 128
Now let's create a map of string and Adorama. 129
So the value will also be map I guess to do that. 130
So let's say map. 131
Now this map let's have string. 132
So earlier it was name and age. 133
Now here let's say this is the name. 134
And here will how a list of key value pairs. 135
So this would be some profile information of each user. 136
So we can now age department and stuff like that. 137
OK. 138
So this earlier we had only h. 139
So one value was good enough. 140
But since the how we want to maintain a list of values as part of the profile let's maned Let's use 141
a map here. 142
But instead of integer here let's say object so that we can store you know if it is an age it could 143
be integer if it is city to be string. 144
Ok so let's use a more generic object here. 145
It's called This Prufrock user profile. 146
OK. 147
So next steps just create this object here for this. 148
So let's just copy this. 149
It's called a profile 150
not its actual values 151
that department says. 152
And it's also the city of the user that say New York. 153
OK so that's that. 154
And next we need to add it to user profile. 155
Under law you would be fine. 156
So that's it. 157
And let's also create one more user to use the same profile variable that age. 158
Roger a destroying design department we can be right. 159
Yes you are. 160
And let's just put the second user let's it's that Raj. 161
So those are the two users. 162
Let's just go ahead and bring them 163
. 164
So as you can see it is printing the values so first one is John. 165
This is the key and this is the value which is Nestor's map. 166
OK Sambit here Raj under-invested map. 167
OK. 168
Now let's just go ahead and just retrieve the value of the information about about the first user who 169
John. 170
So let's just say 171
say profile one equal to 172
get would be John. 173
So the type here would be map all string an object and that's the profile of the user John. 174
So let's just go ahead and see and age. 175
Let's just get the age value. 176
So we need to do profile one not get of age but this one returns an object. 177
So we need to downcast it because it's an object here and you're not allowed to just simply print it 178
out age. 179
That's it. 180
So it's run out. 181
And as you can see the age 25 has been extracted from the profile and it's printed here. 182
So that's about it. 183
So that's the hash map. 184
So I would like to give a small exercise for you. 185
Just go ahead and try. 186
There is a second constructor in the constructor that we used was a constructor that is also of the 187
constructor which takes another map. 188
And it just creates a new map. 189
OK. 190
So I would like you to look at the API and try that out. 191
And also we did not try to put all matter. 192
You can also try that and you can also try all other matters like clear and size and there is one View-Master 193
values which would return the collection of all the values in the hash map. 194
So you can also try that out. 195
So in the next lesson we will extend this demo because we want we said that we the keys. 196
Generally it has to be immutable objects. 197
But if it if it is a mutable object then we need to be very careful. 198
So we will do all of that and we will see what kind of problem we can have. 199
If you use a mutable object as the key. 200
So that will be the next lesson. 201
Thank you. 202
Happy coding. 203
And you can find the code in the resources section