File size: 22,230 Bytes
2e5dac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
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.