java-development-for-beginners-learnit / 19 - Java Collections Framework /019 Set Implementations (HashSet practice) & java.util.Colelctions class_en.srt
Tan115's picture
Add files using upload-large-folder tool
2e5dac7 verified
Raw
History Blame Contribute Delete
22.2 kB
1
00:00:06,000 --> 00:00:06,000
Hello.
2
00:00:06,000 --> 00:00:12,000
Yes, students, today we're going to learn implementations of set interface and talk about utility
3
00:00:12,000 --> 00:00:18,000
methods from Collection's class on how that works inside when we review hashad source code.
4
00:00:18,000 --> 00:00:24,000
After this lesson, you'll understand how set are different from list will answer what mechanisms is
5
00:00:24,000 --> 00:00:27,000
used to keep only unique elements inside the set.
6
00:00:28,000 --> 00:00:33,000
I'm going to show you straight safe implementation of that interface and we'll have practice today.
7
00:00:33,000 --> 00:00:38,000
And at the end of the lesson, we'll review with your class, with utility methods.
8
00:00:38,000 --> 00:00:39,000
That is collections class.
9
00:00:39,000 --> 00:00:46,000
Let's start, as you remember, said Interface is located in the collection Iraqi and does not introduce
10
00:00:46,000 --> 00:00:47,000
any new methods.
11
00:00:47,000 --> 00:00:54,000
Do not forget that that is not a list and said by default does not guarantee any order of elements and
12
00:00:54,000 --> 00:00:56,000
index access to the elements.
13
00:00:56,000 --> 00:01:03,000
The main feature of that interface and all of its implementations is that it contains only unique elements
14
00:01:04,000 --> 00:01:10,000
that various business cases when you need to use set, for example, you are retrieving data from different
15
00:01:10,000 --> 00:01:16,000
data sources and you know that data might be duplicated, but you don't need any duplication in this
16
00:01:16,000 --> 00:01:17,000
case.
17
00:01:17,000 --> 00:01:24,000
You just add elements to the set that takes control over all duplicates, or in the case you interact
18
00:01:24,000 --> 00:01:29,000
with user and want to avoid any duplication in information that was provided by user.
19
00:01:29,000 --> 00:01:35,000
You you said there are really a lot of cases where you can understand that you need set.
20
00:01:35,000 --> 00:01:41,000
And now imagine that if you would be asked to implement something that keeps only unique elements and
21
00:01:41,000 --> 00:01:43,000
contain it, how would you do that?
22
00:01:44,000 --> 00:01:49,000
Would you use any other containers that you already know something that's already stores?
23
00:01:49,000 --> 00:01:50,000
Only unique elements?
24
00:01:51,000 --> 00:01:58,000
Try to think for a second before I would give my answer on map implementation store set of keys.
25
00:01:58,000 --> 00:01:59,000
And each key is unique.
26
00:02:00,000 --> 00:02:02,000
There is no sense to have duplicated keys.
27
00:02:02,000 --> 00:02:06,000
That's why in the case we would need to implement set interface.
28
00:02:06,000 --> 00:02:12,000
Most likely we would use any map implementation and delegate method invocations to the map.
29
00:02:12,000 --> 00:02:19,000
But from the side perspective, it would look like we interact only with keys and we have regular collection,
30
00:02:19,000 --> 00:02:20,000
but not the map.
31
00:02:21,000 --> 00:02:24,000
Let's review the most popular implementations of that interface.
32
00:02:25,000 --> 00:02:31,000
I would say that one of the most popular set implementations is a hash set based on my experience and
33
00:02:31,000 --> 00:02:35,000
most of the cases when I need that implementation, I use hash set.
34
00:02:35,000 --> 00:02:42,000
It extends abstract set that gives implementation to some basic operations, namely two equals hash
35
00:02:42,000 --> 00:02:45,000
code and remove all methods and implements.
36
00:02:45,000 --> 00:02:52,000
Set interface hash that is based on the hash map hash that aggregates map inside.
37
00:02:52,000 --> 00:02:58,000
And we'll see this today when we investigate the source code and other popular implementation of that
38
00:02:58,000 --> 00:02:59,000
interface.
39
00:02:59,000 --> 00:03:03,000
Israel hash set this one user linked hash map inside.
40
00:03:03,000 --> 00:03:10,000
It extends hash said main feature of link hash said is that it provides predictable iteration order.
41
00:03:10,000 --> 00:03:14,000
The next set implementation is called Resat.
42
00:03:14,000 --> 00:03:16,000
Can you guess what it is based on?
43
00:03:16,000 --> 00:03:17,000
You are right.
44
00:03:17,000 --> 00:03:23,000
We said use a stream mapping site to keep all elements sorted according to natural ordering, what with
45
00:03:23,000 --> 00:03:27,000
the help of comparator that was provided during the construction of the object.
46
00:03:28,000 --> 00:03:35,000
It also extends Absaroka set and implements navigable set interface that in turn extend sort of set
47
00:03:35,000 --> 00:03:35,000
interface.
48
00:03:36,000 --> 00:03:41,000
And I really believe there will be no need to investigate these interfaces from scratch.
49
00:03:41,000 --> 00:03:46,000
Take into account we reviewed in detail, sorted map and navigable map interfaces.
50
00:03:46,000 --> 00:03:48,000
One will look at the source code.
51
00:03:48,000 --> 00:03:54,000
You understand that implementation of set interface is pretty straightforward for you in case you learned
52
00:03:54,000 --> 00:03:54,000
maps.
53
00:03:54,000 --> 00:03:59,000
Well, that's why I always, along with my students, map interfaces.
54
00:03:59,000 --> 00:04:05,000
First, if you need thread safe implementation of set, you have to choose among different options.
55
00:04:05,000 --> 00:04:12,000
Some of them are Kapyong Right array set the said that is based on compound right or at least that keep
56
00:04:12,000 --> 00:04:15,000
on the unique elements in container concurrence.
57
00:04:15,000 --> 00:04:18,000
Keep list set that is based on concurrence.
58
00:04:18,000 --> 00:04:24,000
Keep list map where elements are sorted according to natural ordering or according to the comparator.
59
00:04:24,000 --> 00:04:28,000
In other words, this is thread safe implementation of navigable set.
60
00:04:28,000 --> 00:04:34,000
And there's also one more option is to use utility methods from collections glass that will churn any
61
00:04:34,000 --> 00:04:35,000
map to set.
62
00:04:36,000 --> 00:04:42,000
There's also one more implementation of set interface that is usually mentioned in books that is and
63
00:04:42,000 --> 00:04:42,000
set.
64
00:04:43,000 --> 00:04:46,000
This type exists specifically to store only enum elements.
65
00:04:47,000 --> 00:04:51,000
To be honest, I didn't use it very often, but it worth to match it.
66
00:04:52,000 --> 00:04:58,000
OK, it looks like now you are familiar with the set Iraqi and now you're ready for the demo, and we'll
67
00:04:58,000 --> 00:05:02,000
start our demo today from overview of hash that Scott.
68
00:05:03,000 --> 00:05:09,000
Here you can see the property of Type Hashmat Parametrized by any type four keys and object for values.
69
00:05:10,000 --> 00:05:16,000
Also, you can see here a constant object that is damage value that will be used as a value that will
70
00:05:16,000 --> 00:05:19,000
be associated with all keys in this map.
71
00:05:20,000 --> 00:05:24,000
Take into account and have said we are not bothering about the values in the internal map.
72
00:05:24,000 --> 00:05:30,000
There is a way to save some memory by putting always the same object as a value into the internal map.
73
00:05:31,000 --> 00:05:32,000
Does it make sense?
74
00:05:32,000 --> 00:05:39,000
You can find different variations of constructors here, but also considering the fact that Hashad is
75
00:05:39,000 --> 00:05:43,000
based on the Hashmat, that in turn is based on the hash table.
76
00:05:43,000 --> 00:05:48,000
We can specify initial capacity and load factor in case we want to change performance a bit.
77
00:05:49,000 --> 00:05:55,000
And after that you can see that implementation of all methods just delegated the course to the map and
78
00:05:55,000 --> 00:05:57,000
adopt interface when it is needed.
79
00:05:58,000 --> 00:06:01,000
Here is a size method that just calls map size.
80
00:06:01,000 --> 00:06:07,000
Inside here is is empty masset that just cause map is empty inside and so on.
81
00:06:08,000 --> 00:06:10,000
Let's look at at method here.
82
00:06:10,000 --> 00:06:13,000
You can see that we call Puth Masset from MAP.
83
00:06:13,000 --> 00:06:20,000
We put the element that was passed as an argument to this set into its map and associate it with the
84
00:06:20,000 --> 00:06:24,000
constant dummy object that we saw at the beginning of our view here.
85
00:06:24,000 --> 00:06:30,000
According to collection interface at Mass, it should return true in the case element was inserted and
86
00:06:30,000 --> 00:06:34,000
force in case element was not added to collection for some reasons.
87
00:06:34,000 --> 00:06:40,000
I believe you remember that put method in map returns the last value associated with this key.
88
00:06:40,000 --> 00:06:43,000
And in case this is in Uki, it returns now.
89
00:06:43,000 --> 00:06:48,000
So in the case this statement is true, that means an element was added to set.
90
00:06:48,000 --> 00:06:52,000
Does it make sense how this makes things clearer?
91
00:06:52,000 --> 00:06:58,000
Let's quickly review Sodded set a navigable set interfaces to make sure that these interfaces are pretty
92
00:06:58,000 --> 00:07:01,000
similar to sorted map and navigable map.
93
00:07:01,000 --> 00:07:04,000
Here is the source code of sorted set interface.
94
00:07:04,000 --> 00:07:10,000
You can see pretty familiar to you interface that has methods for retrieving first and last element
95
00:07:10,000 --> 00:07:13,000
from container and creating different variations of a subset.
96
00:07:14,000 --> 00:07:16,000
Now let's open navigable set interface.
97
00:07:16,000 --> 00:07:22,000
If you remember navigable map interface, you already can see that massas name are familiar to you.
98
00:07:23,000 --> 00:07:29,000
The only difference is there are no methods that returns entry type, but the rest is really similar.
99
00:07:30,000 --> 00:07:33,000
Now let's practice a bit to see how that actually works.
100
00:07:34,000 --> 00:07:38,000
I created this demo file to show you how you can interact with anisette implementation.
101
00:07:39,000 --> 00:07:43,000
Here you can see that I declare the variable of type set parametrized by integer.
102
00:07:44,000 --> 00:07:49,000
That means calculator will watch for me that I would add only integers in this set.
103
00:07:50,000 --> 00:07:56,000
OK, let me add a few elements to the set here and print the output of each at Abberation.
104
00:07:56,000 --> 00:08:04,000
After that, I will print all set to console, let me run this program and you can see that when I added
105
00:08:04,000 --> 00:08:10,000
one the first time it was successfully inserted, but when I tried to insert one, the second time,
106
00:08:10,000 --> 00:08:13,000
it wasn't inserted and we got false.
107
00:08:13,000 --> 00:08:15,000
Two and three were added successfully.
108
00:08:15,000 --> 00:08:17,000
And here you can see my set of numbers.
109
00:08:18,000 --> 00:08:23,000
Only three unique numbers are here without any duplication, just one common here.
110
00:08:23,000 --> 00:08:29,000
I believe you understand that uniqueness of elements is determined based on the hash code and equals
111
00:08:29,000 --> 00:08:33,000
matters, because these two methods help us to place element in hash table.
112
00:08:34,000 --> 00:08:41,000
Let me bring you this great new set parametrized by user type in case you missed homework about online
113
00:08:41,000 --> 00:08:41,000
store.
114
00:08:41,000 --> 00:08:44,000
That is our type from the online store application.
115
00:08:44,000 --> 00:08:52,000
I create two objects of default user type that are the same, but the default user class does not override
116
00:08:52,000 --> 00:08:53,000
hash code and equals masses.
117
00:08:54,000 --> 00:09:01,000
That's why, even in case logically these two objects are equal, set will store both of these elements
118
00:09:01,000 --> 00:09:08,000
and here print it all set elements to console and you can see that both users are in the container even
119
00:09:08,000 --> 00:09:08,000
despised.
120
00:09:08,000 --> 00:09:10,000
They seem to be logical the same.
121
00:09:11,000 --> 00:09:13,000
Now, let me show you another example.
122
00:09:13,000 --> 00:09:18,000
I agree to objects that overwrites equals and hash code matter.
123
00:09:18,000 --> 00:09:24,000
If you remember, during one of the lessons we created the user for hash tables, and this is exactly
124
00:09:24,000 --> 00:09:27,000
the object from our lesson about hash tables.
125
00:09:27,000 --> 00:09:31,000
If you remember here, you can see equals and hash code overridden.
126
00:09:32,000 --> 00:09:37,000
In case I would put these two objects in the set, only one will be added.
127
00:09:39,000 --> 00:09:44,000
Here, I change the first name to William and set a different idea to distinguish users without hash
128
00:09:44,000 --> 00:09:49,000
code and equals methods, and we use these methods of freedom here.
129
00:09:49,000 --> 00:09:55,000
When I printed all users to console one by one, you can see that only one of them was added here.
130
00:09:56,000 --> 00:10:02,000
Also, the things that you have already noticed is that you can use Iterator and foreach loop to iterate
131
00:10:02,000 --> 00:10:05,000
over all elements in set take into account.
132
00:10:05,000 --> 00:10:09,000
It is also implements iterable interface and has its iterator.
133
00:10:10,000 --> 00:10:16,000
Now, you know how to use sets in your work, and the last thing that I wanted to share with you today
134
00:10:16,000 --> 00:10:17,000
is collections glass.
135
00:10:18,000 --> 00:10:19,000
Why do we need it?
136
00:10:19,000 --> 00:10:21,000
Sometimes we need utility mass.
137
00:10:21,000 --> 00:10:22,000
It's like sort of a collection.
138
00:10:22,000 --> 00:10:28,000
For example, let me open the source code of this glass and explore it together with you.
139
00:10:28,000 --> 00:10:30,000
It has really a lot of the masses.
140
00:10:31,000 --> 00:10:36,000
I will not provide my comments for all the masses listed here, but I will tell Fumaroles about only
141
00:10:36,000 --> 00:10:38,000
the most popular ones in my opinion.
142
00:10:39,000 --> 00:10:44,000
And I encourage you to use a source code of this class after the lesson and practice and meet with this
143
00:10:44,000 --> 00:10:46,000
class by calling different methods.
144
00:10:47,000 --> 00:10:48,000
Here we can see Masset.
145
00:10:49,000 --> 00:10:55,000
So sometimes instead of calling sort method on the least, we can call source method of collections
146
00:10:55,000 --> 00:10:59,000
class, you can reverse order of elements in your list if you wish.
147
00:10:59,000 --> 00:11:04,000
You can call reverse Masset and pass reference to any object there if you wish.
148
00:11:05,000 --> 00:11:10,000
Here is a shuffle method that you saw we used for demo purposes in one of the lessons and past.
149
00:11:11,000 --> 00:11:17,000
Also, you can see some of Masset to swap elements by specifying position indexes or field method to
150
00:11:17,000 --> 00:11:19,000
feel content with the specific elements.
151
00:11:19,000 --> 00:11:25,000
He is very useful copying method that allows you to copy list methods to find the least.
152
00:11:25,000 --> 00:11:29,000
And the greatest elements in collection are also here and here.
153
00:11:29,000 --> 00:11:35,000
And one of the coolest methods you can take is a list or said unnavigable, said unnavigable map or
154
00:11:35,000 --> 00:11:39,000
anything other to get unverifiable object.
155
00:11:39,000 --> 00:11:47,000
In other words, the methods that can rub your objects with unquantifiable ropa when in you just imagine
156
00:11:47,000 --> 00:11:53,000
that you implement a method for cars or objects in online store and you don't want to return products
157
00:11:53,000 --> 00:11:59,000
of cards and to let anyone else in other parts of the program to add products directly to the list.
158
00:11:59,000 --> 00:12:00,000
Does it make sense?
159
00:12:01,000 --> 00:12:07,000
What you want instead is that in case somebody wants to add product to the cart, they have to use that
160
00:12:07,000 --> 00:12:10,000
product that contains additional business logic.
161
00:12:10,000 --> 00:12:14,000
It can be tax free calculation or discount of card calculation.
162
00:12:14,000 --> 00:12:20,000
And now we can say that we return on modifiable list because of clients of our cars.
163
00:12:20,000 --> 00:12:24,000
Want to investigate what products in it they can look at them.
164
00:12:24,000 --> 00:12:25,000
There is no harm in this.
165
00:12:26,000 --> 00:12:31,000
But in case they would like to modify the cards, they will catch and unsupported separation exception,
166
00:12:31,000 --> 00:12:34,000
the sad creation of wrappers for one modifiable collections.
167
00:12:34,000 --> 00:12:39,000
We also can create wrappers to turn our regular containers to thread safe containers.
168
00:12:40,000 --> 00:12:45,000
You can find a bunch of methods here that can return to synchronized list or map or set or anything
169
00:12:45,000 --> 00:12:46,000
else.
170
00:12:46,000 --> 00:12:50,000
If you wish, let me open another my file to show you a few more examples.
171
00:12:51,000 --> 00:12:51,000
Collections.
172
00:12:51,000 --> 00:12:55,000
Glass also has Masset that can turn any map implementation to set.
173
00:12:56,000 --> 00:13:01,000
For example, I can create that safe set on the basis of concurrent Hashmat just like this.
174
00:13:02,000 --> 00:13:05,000
Take into account en methods are static and collections class.
175
00:13:05,000 --> 00:13:12,000
I write collections class name after that I call method new said from map and parse the concurrent hash
176
00:13:12,000 --> 00:13:16,000
map object where keys are parametrized with the same type as my set.
177
00:13:17,000 --> 00:13:23,000
According to interface, the values should be parametrized by boolean type and assign this object to
178
00:13:23,000 --> 00:13:23,000
my set variable.
179
00:13:24,000 --> 00:13:30,000
When you might want to use this, for example, you're extremely satisfied with the performance of concurrent
180
00:13:30,000 --> 00:13:35,000
Hashmat and you don't want to use generalized wrapper suggested by collections class.
181
00:13:35,000 --> 00:13:41,000
That's why you might want to use this method to create set implementation exactly on the basis of specific
182
00:13:41,000 --> 00:13:42,000
map implementation.
183
00:13:43,000 --> 00:13:44,000
Does it make sense?
184
00:13:45,000 --> 00:13:48,000
And the last but not the least super useful feature of collections.
185
00:13:48,000 --> 00:13:50,000
Glass is empty constants.
186
00:13:51,000 --> 00:13:55,000
To make you understand what the benefit we can get, I prepared example here for you.
187
00:13:56,000 --> 00:14:01,000
Imagine that you have some in a program that is supposed to return to your list of products that are
188
00:14:01,000 --> 00:14:07,000
associated with a specific course, i.e. let's call this method get products by Karzai.
189
00:14:07,000 --> 00:14:08,000
Awesome.
190
00:14:08,000 --> 00:14:15,000
Now we are trying to retrieve products from database by card idea and imagine we got now in response
191
00:14:15,000 --> 00:14:18,000
because there are no products associated with this card.
192
00:14:18,000 --> 00:14:24,000
Remember, as a rule, my return now from any method you increase the risk of facing with no point the
193
00:14:24,000 --> 00:14:29,000
exception in case somebody would interact with the result of this method.
194
00:14:29,000 --> 00:14:32,000
In other parts of the program, what is recommended?
195
00:14:32,000 --> 00:14:37,000
It is recommended to return empty lists instead to avoid Northpoint exception.
196
00:14:37,000 --> 00:14:41,000
OK, let's look at this option now.
197
00:14:41,000 --> 00:14:45,000
Imagine that you have huge online store and this method is invoked very often.
198
00:14:45,000 --> 00:14:52,000
This will lead you to creation of thousands of objects of our least the need to load your memory even
199
00:14:52,000 --> 00:14:53,000
with empty lists.
200
00:14:53,000 --> 00:14:54,000
I don't think so.
201
00:14:54,000 --> 00:14:56,000
But what is the best option?
202
00:14:56,000 --> 00:15:02,000
In my opinion, the best option in this case is to use predefined constants from collections.
203
00:15:02,000 --> 00:15:03,000
Glass Empty List.
204
00:15:03,000 --> 00:15:09,000
Masset will return you parametrized collection based on the empty list constant if you need you.
205
00:15:09,000 --> 00:15:15,000
And also use empty, sad or empty map, for example, hope this information will help you a lot in the
206
00:15:15,000 --> 00:15:16,000
future.
207
00:15:16,000 --> 00:15:21,000
OK, now I believe we can recap the things that we have learned today.
208
00:15:22,000 --> 00:15:28,000
Today, we learned that Iraqi you learned the main interfaces and the most popular implementations of
209
00:15:28,000 --> 00:15:32,000
that interface after that review, the source code of Hashmat.
210
00:15:32,000 --> 00:15:36,000
And so how set implementations use map implementations inside.
211
00:15:36,000 --> 00:15:42,000
We also had the practice with Hash said, and you saw how hash that keeps only unique elements.
212
00:15:42,000 --> 00:15:47,000
At the end of the lesson, we learned a lot of utility methods from Collection's class.
213
00:15:47,000 --> 00:15:52,000
You also learn best practices of using unverifiable and synchronized robbers.
214
00:15:52,000 --> 00:15:56,000
You also know when and why you have to use empty containers.
215
00:15:57,000 --> 00:15:58,000
Thanks a lot for your attention.
216
00:15:59,000 --> 00:16:02,000
Have a great day and see you in the next lesson.