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