Tan115's picture
Add files using upload-large-folder tool
2e5dac7 verified
Raw
History Blame Contribute Delete
21.7 kB
1
00:00:06,000 --> 00:00:11,000
Hello, dear students, today we have important topic we are going to review another Iraq of Java collections
2
00:00:11,000 --> 00:00:14,000
framework that is MOP Iraqi today.
3
00:00:14,000 --> 00:00:16,000
We are going to run map interface.
4
00:00:16,000 --> 00:00:20,000
We are also going to review such interfaces like sorted map and navigable map.
5
00:00:21,000 --> 00:00:26,000
I'm going to hold overview of implementations of map interface and what features they have.
6
00:00:27,000 --> 00:00:32,000
Also, we'll talk about what the dictionary is and how hash tablecloth is different from hash map.
7
00:00:32,000 --> 00:00:37,000
After we learn, Ceri will focus on map interface and its methods.
8
00:00:37,000 --> 00:00:39,000
Your review of map interface.
9
00:00:39,000 --> 00:00:41,000
We also will learn what entry type is.
10
00:00:42,000 --> 00:00:46,000
As you can see that we have a lot of things to learn in our plans.
11
00:00:46,000 --> 00:00:53,000
Let's start and I'd like to start with you from the overview of Map Iraqi first and the key features
12
00:00:53,000 --> 00:00:53,000
of MAP.
13
00:00:54,000 --> 00:01:00,000
And after we learn this, we are going to dive deeper in the details to investigate each interface in
14
00:01:00,000 --> 00:01:04,000
particular on the top of our Iraqi, we have map interface.
15
00:01:04,000 --> 00:01:07,000
This type exists to map keys to values.
16
00:01:07,000 --> 00:01:11,000
All keys are unique, various may be duplicated.
17
00:01:11,000 --> 00:01:15,000
And there are a lot of places and our programs where we can apply map implementations.
18
00:01:16,000 --> 00:01:22,000
If you remember on one of the lessons I showed you example of mobile app for sports betting tips, for
19
00:01:22,000 --> 00:01:26,000
example, we can receive from server a lot of sports events.
20
00:01:26,000 --> 00:01:33,000
After that, we can group them by date and your keyword would be the specific date and the value would
21
00:01:33,000 --> 00:01:38,000
be a collection of sporting events on this day or another example, recently you have implemented Home
22
00:01:38,000 --> 00:01:40,000
Task Help Desk.
23
00:01:40,000 --> 00:01:41,000
In that task.
24
00:01:41,000 --> 00:01:44,000
You also could use map implementations to map support.
25
00:01:44,000 --> 00:01:47,000
Request type was its priority.
26
00:01:47,000 --> 00:01:53,000
In this case, support request type would be the key and it will be unique and priority would be the
27
00:01:53,000 --> 00:01:55,000
value and can be duplicated.
28
00:01:56,000 --> 00:02:01,000
And I keep creating examples on the fly because there really are a lot of cases where you need some
29
00:02:01,000 --> 00:02:04,000
data structure where you would store key value pass.
30
00:02:04,000 --> 00:02:07,000
OK, so we really understood why we need map.
31
00:02:07,000 --> 00:02:14,000
Now let's understand what specific different implementations of map interface have abstract map use
32
00:02:14,000 --> 00:02:21,000
implementations for some of the methods of map interface, hash map implementation of map interface.
33
00:02:21,000 --> 00:02:27,000
IT stores element using the mechanism of hash tables that guarantees constant amount of time for each
34
00:02:27,000 --> 00:02:29,000
region and place elements.
35
00:02:29,000 --> 00:02:33,000
That's why everyone loves hash map linked hash map.
36
00:02:33,000 --> 00:02:38,000
Besides storing elements also memorized the sequence of adding entries in the map.
37
00:02:39,000 --> 00:02:46,000
So the order of elements during the iteration is predictable because linked hash map maintains the list
38
00:02:46,000 --> 00:02:48,000
ran through all of its entries.
39
00:02:48,000 --> 00:02:55,000
If we need thread safe implementation of the map, we would opt for concurrent hash map that implements
40
00:02:55,000 --> 00:02:56,000
concurrent map.
41
00:02:56,000 --> 00:02:58,000
That in turn extends map interface.
42
00:02:59,000 --> 00:03:03,000
We have a few more interfaces that extend map interface.
43
00:03:03,000 --> 00:03:10,000
Zaya sorted map and navigable map sort of map provides ordering of a map based on its case.
44
00:03:11,000 --> 00:03:17,000
The map is ordered according to the natural order of its keys that is defined according to implementation
45
00:03:17,000 --> 00:03:25,000
of comparable interface or alternatively, Solin order may be defined by comparator navigable map extensors
46
00:03:25,000 --> 00:03:31,000
map and provides developers with possibility to return the closest matches for given such targets.
47
00:03:31,000 --> 00:03:38,000
The most popular implementations of this interface are three map and concurrence key place map for multithreaded
48
00:03:38,000 --> 00:03:39,000
environment.
49
00:03:40,000 --> 00:03:42,000
We also have APSA class dictionary.
50
00:03:42,000 --> 00:03:48,000
How it is different from the map interface you can reclass was created in Java version one and supposed
51
00:03:48,000 --> 00:03:52,000
to be a parent class for all types that map keys to values.
52
00:03:52,000 --> 00:03:58,000
Since multiple inheritance in Java is not supported by the map interface was introduced in Java version
53
00:03:58,000 --> 00:03:58,000
two.
54
00:03:59,000 --> 00:04:05,000
So I would say that dictionary is an old version of map interface and that is considered as an obsolete
55
00:04:05,000 --> 00:04:07,000
and use of map is preferred.
56
00:04:08,000 --> 00:04:13,000
But still, you might be asked about dictionary during the interview and for your common Java literacy,
57
00:04:13,000 --> 00:04:15,000
you have to be aware about it.
58
00:04:16,000 --> 00:04:21,000
Hash table is a concrete class that extends the dictionary type and internments map interface.
59
00:04:22,000 --> 00:04:27,000
It was considered as a thread safe version of hash map unless concurrent hash map was introduced in
60
00:04:27,000 --> 00:04:33,000
Java version five, the synchronization mechanism is not so efficient as it is implemented in concurrent
61
00:04:33,000 --> 00:04:37,000
Hashmat, in case you are not familiar with the trending topic.
62
00:04:37,000 --> 00:04:42,000
That would explain the difference in the implementation of low interest rate and support in these classes.
63
00:04:42,000 --> 00:04:46,000
In simple words, hash table closes the door in your home.
64
00:04:46,000 --> 00:04:53,000
Concurrent hash map closes a separate doors in your home to allow everyone to walk around the home until
65
00:04:53,000 --> 00:04:54,000
only one room is closed.
66
00:04:55,000 --> 00:05:01,000
Can you feel the difference in implementation of concurrency support in these two classes and meet later
67
00:05:01,000 --> 00:05:02,000
in the separate lessons?
68
00:05:02,000 --> 00:05:08,000
We are going to learn multithreaded together and we will get back to a thread safe map implementations.
69
00:05:09,000 --> 00:05:11,000
That's how MAP Iraqis look like.
70
00:05:11,000 --> 00:05:16,000
Before we proceed with the details, I want to finish the topic of differences between hash map and
71
00:05:16,000 --> 00:05:17,000
hash table.
72
00:05:18,000 --> 00:05:24,000
So the first thing that is different we already discussed on MassArt and hash table are synchronized.
73
00:05:24,000 --> 00:05:28,000
And that's why hash table is considered to be a thread safe map.
74
00:05:28,000 --> 00:05:33,000
Hash table also does not allow news, a new case nor new values.
75
00:05:33,000 --> 00:05:39,000
On the other hand, hash map allows us to store one key and multiple values.
76
00:05:39,000 --> 00:05:43,000
Hash table is a legacy class, whereas hash map is newer.
77
00:05:44,000 --> 00:05:50,000
And now I suggest opening the source code of map interface to learn that the communication and methods
78
00:05:50,000 --> 00:05:53,000
of this interface here is map interface.
79
00:05:53,000 --> 00:06:00,000
Pay attention to this interface doesn't extend iterable interface like collection while review and map
80
00:06:00,000 --> 00:06:00,000
interface.
81
00:06:00,000 --> 00:06:06,000
I'm going to use and work by saying entry, I would always mean the key value pair.
82
00:06:06,000 --> 00:06:11,000
Is it clear, for example, size returns, amount of entries in the map.
83
00:06:12,000 --> 00:06:16,000
In other words, a number of key value pairs is empty.
84
00:06:16,000 --> 00:06:18,000
This is self-described Macit.
85
00:06:18,000 --> 00:06:25,000
It checks whether the current map is empty or no, contains key verifies whether a map contains specific
86
00:06:25,000 --> 00:06:32,000
contains value, similar method to check value in map, yet method to get value from the map we have
87
00:06:32,000 --> 00:06:32,000
to use.
88
00:06:33,000 --> 00:06:37,000
So we ask you to get Masset to retrieve our value.
89
00:06:37,000 --> 00:06:39,000
There is no method in map.
90
00:06:39,000 --> 00:06:41,000
Instead, we have put method.
91
00:06:41,000 --> 00:06:44,000
We can put key value pair in map.
92
00:06:44,000 --> 00:06:47,000
That's why put method takes two arguments.
93
00:06:48,000 --> 00:06:50,000
Pay attention to the return tab here.
94
00:06:50,000 --> 00:06:56,000
Imagine that you put in map integer value one and some customer reference type user as a value to the
95
00:06:56,000 --> 00:07:01,000
map in this case, but will return now and now.
96
00:07:01,000 --> 00:07:07,000
Imagine that you put new value but with the same key you call put MassArt again was integer value one
97
00:07:07,000 --> 00:07:09,000
and new user object.
98
00:07:09,000 --> 00:07:15,000
In this case, both method will return the value that was previously associated with the key.
99
00:07:15,000 --> 00:07:16,000
That means previous user.
100
00:07:17,000 --> 00:07:18,000
Does it make sense?
101
00:07:18,000 --> 00:07:24,000
If you want to remove some entry from the map, you have to call remove Masset and Paskey as a method
102
00:07:24,000 --> 00:07:29,000
argument map interface, declare methods for bulk operations, for example.
103
00:07:29,000 --> 00:07:33,000
Put all method can add all entries from one object to another.
104
00:07:34,000 --> 00:07:35,000
Clear message.
105
00:07:35,000 --> 00:07:38,000
Remove all mappings from the current map object.
106
00:07:38,000 --> 00:07:44,000
He has said massive returns contain the offset type that contains only keys from this map object.
107
00:07:44,000 --> 00:07:45,000
Why set?
108
00:07:46,000 --> 00:07:48,000
Because all keys are unique and map.
109
00:07:48,000 --> 00:07:51,000
That's why I said type is returned.
110
00:07:51,000 --> 00:07:57,000
In case we want to retrieve all whaler's from the map, we have to call values method that returns collection
111
00:07:57,000 --> 00:07:58,000
of elements.
112
00:07:58,000 --> 00:07:59,000
Why collection?
113
00:08:00,000 --> 00:08:03,000
Because values are not unique and collection is returned.
114
00:08:04,000 --> 00:08:10,000
Here's one more very useful method and set this massive return set of entries of this map.
115
00:08:10,000 --> 00:08:15,000
Each key value pair is represented as a single object of type entry.
116
00:08:15,000 --> 00:08:17,000
You really understand why set returned?
117
00:08:18,000 --> 00:08:23,000
Because each key value pair is unique and three is in our interface in map.
118
00:08:24,000 --> 00:08:25,000
Let's look at it.
119
00:08:25,000 --> 00:08:29,000
Entry type has to match assets that are used more often than others.
120
00:08:29,000 --> 00:08:32,000
They are yet key and get value.
121
00:08:32,000 --> 00:08:39,000
For example, we can iterate over a set of entries in foreach loop and in the foreach loop body we can
122
00:08:39,000 --> 00:08:42,000
use reference to the entry object together is a key or value.
123
00:08:43,000 --> 00:08:45,000
It depends on what exactly we need.
124
00:08:45,000 --> 00:08:46,000
According to our logic.
125
00:08:47,000 --> 00:08:51,000
There are also other methods that data on my opinion are not used very often.
126
00:08:51,000 --> 00:08:53,000
For example, set value.
127
00:08:53,000 --> 00:08:57,000
It allows us to set value associated with the current key in the entry.
128
00:08:57,000 --> 00:09:03,000
In my opinion, if there would be such need, I mean to update the value associated with a specific
129
00:09:03,000 --> 00:09:09,000
key, most likely you want to do this with entry time, but instead we'll just call good method with
130
00:09:09,000 --> 00:09:12,000
the same key on map object to update the value.
131
00:09:13,000 --> 00:09:18,000
But probably one day you would face with the case that while iterating over entries, you need to implement
132
00:09:18,000 --> 00:09:24,000
logic that under some specific conditions you have to update value that is associated with a specific
133
00:09:24,000 --> 00:09:25,000
key.
134
00:09:25,000 --> 00:09:29,000
And for this case, you already know what method can help you with that.
135
00:09:30,000 --> 00:09:32,000
You can see equals and hash code.
136
00:09:32,000 --> 00:09:38,000
You already know these methods and also entry has a few interesting static methods.
137
00:09:38,000 --> 00:09:41,000
We are going to use them in our next lessons.
138
00:09:41,000 --> 00:09:41,000
Right now.
139
00:09:41,000 --> 00:09:48,000
Let me explain you and see what I say about we have comparing biochem assets at source entries by case
140
00:09:48,000 --> 00:09:55,000
in case there implement comparable interface and we compare the parameter in case keys don't implement
141
00:09:55,000 --> 00:09:56,000
comparable interface.
142
00:09:57,000 --> 00:10:03,000
These methods return compared to that we can use to search and now attention to search our entries set,
143
00:10:04,000 --> 00:10:06,000
but not elements in map bytes key.
144
00:10:07,000 --> 00:10:12,000
And we also have comparing by value method that returns comparisons that can compare entries set by
145
00:10:12,000 --> 00:10:13,000
values.
146
00:10:13,000 --> 00:10:16,000
Imagine that you want to search your map by.
147
00:10:16,000 --> 00:10:21,000
Well, to be honest, compared to that source, my case is not so cool.
148
00:10:21,000 --> 00:10:21,000
Why?
149
00:10:22,000 --> 00:10:27,000
Because we can create object of three map and put all elements from any map there and elements will
150
00:10:27,000 --> 00:10:31,000
be sorted automatically by case, but sorting by various.
151
00:10:31,000 --> 00:10:36,000
That is really helpful compared to now when we answer it, what entry type is.
152
00:10:36,000 --> 00:10:43,000
Let's investigate what matters are left here in the map interface equals and hash good methods and.
153
00:10:44,000 --> 00:10:49,000
The default message I described, for example, here is interesting and useful message.
154
00:10:49,000 --> 00:10:55,000
Imagine that you need to get away from that, but you don't want to get new in case Element is absent
155
00:10:55,000 --> 00:10:56,000
in map.
156
00:10:56,000 --> 00:11:03,000
Instead, you would like to get some default value we can call yet or default to retrieve element by
157
00:11:03,000 --> 00:11:09,000
key in case and market for such key present or to return default value in case there is no mapping for
158
00:11:09,000 --> 00:11:10,000
such key.
159
00:11:10,000 --> 00:11:17,000
Pay attention to the fact that in case we exist and values that is associated with this key is no value,
160
00:11:18,000 --> 00:11:19,000
then no will be returned.
161
00:11:20,000 --> 00:11:24,000
Default value will be returned only in case there is no such key in the map.
162
00:11:25,000 --> 00:11:30,000
For each matsutake takes function that should be applied for each and every one will return functional
163
00:11:30,000 --> 00:11:31,000
programming with Java.
164
00:11:32,000 --> 00:11:34,000
That will show you how you can apply this method.
165
00:11:35,000 --> 00:11:41,000
Replace all text functions that will be applied to each entry and each entry in turn will be substituted
166
00:11:41,000 --> 00:11:44,000
with the result that will be returned by this function.
167
00:11:44,000 --> 00:11:51,000
But if absent method should be used when you don't want to override the values that is already associated
168
00:11:51,000 --> 00:11:52,000
with a specific key.
169
00:11:52,000 --> 00:11:59,000
So new value will be put into the map only in case there is no entry with such key or keys associated
170
00:11:59,000 --> 00:11:59,000
with no value.
171
00:12:00,000 --> 00:12:05,000
The removal method can help you to remove entry by specifying the key value pair here.
172
00:12:05,000 --> 00:12:06,000
Also replace MassArt.
173
00:12:07,000 --> 00:12:12,000
They are different from Puth method because you can say that you want to replace Element only with a
174
00:12:12,000 --> 00:12:19,000
specific key and with a specific value, or in case of overall that method only in case such key exists.
175
00:12:20,000 --> 00:12:24,000
Comparative absence method allows you to compute value in case he is absent.
176
00:12:24,000 --> 00:12:30,000
We can pass the keys that doesn't exist in map and function that can compute new value.
177
00:12:30,000 --> 00:12:34,000
In case such care exists, the value won't be updated.
178
00:12:34,000 --> 00:12:41,000
Compute if present method takes key and functions that can compute the value based on the current key
179
00:12:41,000 --> 00:12:47,000
and previous value that is associated with it in case he exists and previous value is not.
180
00:12:47,000 --> 00:12:53,000
Now, the result of the function will be associated with this key compute method.
181
00:12:53,000 --> 00:12:59,000
Attempts to compute and mapping for the specified key and its current mapped value is there is no current
182
00:12:59,000 --> 00:12:59,000
mapping.
183
00:13:00,000 --> 00:13:02,000
Mersch method works in the following way.
184
00:13:03,000 --> 00:13:08,000
It is a specified key, is not already associated with a value or is associated with.
185
00:13:08,000 --> 00:13:11,000
Now associate it with the given no new value.
186
00:13:12,000 --> 00:13:19,000
Otherwise replace the associated value with the results of the given remapping function or removes if
187
00:13:19,000 --> 00:13:21,000
the result is now lost for assets.
188
00:13:21,000 --> 00:13:24,000
Require knowledge of functional programming in Java.
189
00:13:24,000 --> 00:13:29,000
That's why they will be reviewed in detail separately in the lessons about functional programming.
190
00:13:30,000 --> 00:13:37,000
Right now it is enough high level understanding what this message do, and now we are done with all
191
00:13:37,000 --> 00:13:42,000
default methods in my interface and it is time to review static methods in this interface.
192
00:13:43,000 --> 00:13:49,000
Here we see overloaded static method with the name of this method, say here since Java version nine
193
00:13:49,000 --> 00:13:51,000
and they can create immutable.
194
00:13:51,000 --> 00:13:56,000
That also means unverifiable map that contains a limited number of mappings.
195
00:13:57,000 --> 00:14:02,000
And you can see that the only difference between all of these methods is a number of mappings that it
196
00:14:02,000 --> 00:14:04,000
takes as a parameter.
197
00:14:05,000 --> 00:14:10,000
Here's a method that takes variable lengths, argument of type entry, and based on these creates and
198
00:14:10,000 --> 00:14:18,000
modifiable map and remasters returns and modifiable entry type based on the key value pair that is passed
199
00:14:18,000 --> 00:14:20,000
to the method, why this method is needed.
200
00:14:20,000 --> 00:14:26,000
This method is needed, for example, for the previous method that we have just discussed, because
201
00:14:26,000 --> 00:14:31,000
you want to eliminate the case when your entries changed because somebody got access to it by reference.
202
00:14:32,000 --> 00:14:35,000
That's why you might want to create an modifiable entry first.
203
00:14:35,000 --> 00:14:37,000
And this method will help you in that.
204
00:14:38,000 --> 00:14:41,000
And the last but not least method for today is copy of.
205
00:14:42,000 --> 00:14:48,000
If we want to create the copy of the map that nobody will be able to modify, we have to call this method
206
00:14:48,000 --> 00:14:52,000
and parse the map that we want to copy as an argument to this method.
207
00:14:53,000 --> 00:14:55,000
That's all method for map interface.
208
00:14:55,000 --> 00:15:00,000
I believe that right now, you know, map interface even better than some senior software engineers.
209
00:15:01,000 --> 00:15:03,000
Let's recap what we have learned today.
210
00:15:03,000 --> 00:15:05,000
Today, we learned Map Iraqi.
211
00:15:05,000 --> 00:15:10,000
We talked about features that are introduced in the implementations of map interface.
212
00:15:11,000 --> 00:15:14,000
We also talked about source, map and navigable map.
213
00:15:14,000 --> 00:15:16,000
We discussed what dictionary is.
214
00:15:17,000 --> 00:15:21,000
Now, you know what the difference is between Hash Table and Hashmat.
215
00:15:21,000 --> 00:15:24,000
After that, we learned methods of map interface.
216
00:15:25,000 --> 00:15:27,000
We also learned what entry type is.
217
00:15:28,000 --> 00:15:29,000
That's all what we have for today.
218
00:15:30,000 --> 00:15:31,000
Thanks a lot for your attention.
219
00:15:31,000 --> 00:15:33,000
See you in the next lesson.