Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:00,238 --> 00:00:02,821
(upbeat music)
2
2
00:00:05,230 --> 00:00:07,070
Well, this is gonna be a short video
3
3
00:00:07,070 --> 00:00:09,730
because the Vector class
4
4
00:00:09,730 --> 00:00:12,940
is essentially a thread safe ArrayList,
5
5
00:00:12,940 --> 00:00:16,010
but Vector actually came first and we're gonna take a
6
6
00:00:16,010 --> 00:00:19,560
look at the Vector Javadoc and we'll see,
7
7
00:00:19,560 --> 00:00:20,740
if we scroll down a bit
8
8
00:00:20,740 --> 00:00:23,820
that Vector has been there since the beginning.
9
9
00:00:23,820 --> 00:00:28,820
It has been in the JDK since JDK 1.0.
10
10
00:00:28,820 --> 00:00:30,920
If we come up to the top here,
11
11
00:00:30,920 --> 00:00:34,203
now, if I go back to ArrayList,
12
12
00:00:36,530 --> 00:00:38,280
so this is the ArrayList class,
13
13
00:00:38,280 --> 00:00:41,210
we'll see that that's, there was only added into
14
14
00:00:41,210 --> 00:00:45,200
the JDK in JDK version 1.2.
15
15
00:00:45,200 --> 00:00:47,940
Now, if you've been using Java as long as I have,
16
16
00:00:47,940 --> 00:00:50,780
you might remember when ArrayList was introduced.
17
17
00:00:50,780 --> 00:00:54,740
Before that, the list go-to class was Vector,
18
18
00:00:54,740 --> 00:00:59,640
and so the question is if Vector was in JDK version 1.0
19
19
00:00:59,640 --> 00:01:01,500
and it has pretty much the same methods
20
20
00:01:01,500 --> 00:01:03,490
as the ArrayList class,
21
21
00:01:03,490 --> 00:01:06,310
why was ArrayList added to the JDK?
22
22
00:01:06,310 --> 00:01:08,923
Well, the answer lies in the documentation.
23
23
00:01:09,780 --> 00:01:12,160
If we scroll back up, we'll see right here
24
24
00:01:12,160 --> 00:01:14,330
in big bold letters for ArrayList,
25
25
00:01:14,330 --> 00:01:17,940
this implementation is not synchronised,
26
26
00:01:17,940 --> 00:01:21,510
and if we go back to the Vector class,
27
27
00:01:21,510 --> 00:01:24,620
it says unlike the new collection implementations,
28
28
00:01:24,620 --> 00:01:27,310
Vector is synchronised.
29
29
00:01:27,310 --> 00:01:29,920
So Vector is thread safe meaning it's okay
30
30
00:01:29,920 --> 00:01:33,000
to use it from different threads without you having
31
31
00:01:33,000 --> 00:01:35,060
to synchronise the code.
32
32
00:01:35,060 --> 00:01:37,380
But ArrayList is not.
33
33
00:01:37,380 --> 00:01:39,670
So if you only read an ArrayList,
34
34
00:01:39,670 --> 00:01:42,180
then it's safe to use it from multiple threads,
35
35
00:01:42,180 --> 00:01:43,670
no data will be clobbered,
36
36
00:01:43,670 --> 00:01:46,350
but if you use ArrayList with multiple threads
37
37
00:01:46,350 --> 00:01:48,610
and one or more of those threads is writing
38
38
00:01:48,610 --> 00:01:50,650
to the ArrayList by adding,
39
39
00:01:50,650 --> 00:01:53,810
deleting, setting or changing objects in the list,
40
40
00:01:53,810 --> 00:01:56,630
then you could run into a conflict.
41
41
00:01:56,630 --> 00:01:58,910
You could have threading conflicts,
42
42
00:01:58,910 --> 00:02:00,070
and so in that case, you'd
43
43
00:02:00,070 --> 00:02:03,080
want to use Vector rather than ArrayList.
44
44
00:02:03,080 --> 00:02:05,420
So if you need thread safety, use Vector.
45
45
00:02:05,420 --> 00:02:06,990
If you don't, use ArrayList.
46
46
00:02:06,990 --> 00:02:09,170
Now, the reason that ArrayList came along
47
47
00:02:09,170 --> 00:02:13,350
is that synchronisation has an overhead involved.
48
48
00:02:13,350 --> 00:02:15,390
So it slows things down,
49
49
00:02:15,390 --> 00:02:18,000
and so when only the Vector class was available
50
50
00:02:18,000 --> 00:02:19,790
and Vector is synchronised,
51
51
00:02:19,790 --> 00:02:23,210
if you wanted to use a list backed by an array
52
52
00:02:23,210 --> 00:02:25,270
and you wanted to use the class in the JDK,
53
53
00:02:25,270 --> 00:02:26,420
you were pretty much forced
54
54
00:02:26,420 --> 00:02:28,620
to use a synchronised class even
55
55
00:02:28,620 --> 00:02:31,270
if you didn't need synchronisation,
56
56
00:02:31,270 --> 00:02:33,300
and so because of that,
57
57
00:02:33,300 --> 00:02:35,450
ArrayList was eventually added to the JDK,
58
58
00:02:35,450 --> 00:02:37,750
so that if you don't need synchronisation,
59
59
00:02:37,750 --> 00:02:39,640
you would have a class backed by an array
60
60
00:02:39,640 --> 00:02:41,510
that you could use that doesn't
61
61
00:02:41,510 --> 00:02:44,580
have the overhead involved with synchronisation,
62
62
00:02:44,580 --> 00:02:46,560
and so that's why ArrayList came along.
63
63
00:02:46,560 --> 00:02:48,860
It was because developers were saying,
64
64
00:02:48,860 --> 00:02:52,140
hey, we like Vector, but what do we do
65
65
00:02:52,140 --> 00:02:53,670
if we don't need synchronisation?
66
66
00:02:53,670 --> 00:02:55,630
We don't want the overhead associated with it,
67
67
00:02:55,630 --> 00:02:58,840
and so a couple of versions later, ArrayList came along.
68
68
00:02:58,840 --> 00:03:01,920
And so here you'll see that the documentation
69
69
00:03:01,920 --> 00:03:04,080
for ArrayList itself says
70
70
00:03:04,080 --> 00:03:06,640
if a thread safe implementation is not needed,
71
71
00:03:06,640 --> 00:03:09,610
it is recommended to use ArrayList.
72
72
00:03:09,610 --> 00:03:12,100
So you only want to use the Vector class
73
73
00:03:12,100 --> 00:03:14,660
if you need synchronisation.
74
74
00:03:14,660 --> 00:03:17,270
If you don't need it and you use the Vector class,
75
75
00:03:17,270 --> 00:03:19,660
you're going to be using a slower implementation
76
76
00:03:19,660 --> 00:03:22,120
for no reason, and that's why you
77
77
00:03:22,120 --> 00:03:24,940
want to use ArrayList unless you need
78
78
00:03:24,940 --> 00:03:27,070
a thread safe implementation.
79
79
00:03:27,070 --> 00:03:28,480
So if you need thread safety,
80
80
00:03:28,480 --> 00:03:31,870
use Vector, if you don't, use ArrayList.
81
81
00:03:31,870 --> 00:03:34,410
Okay, so let's say we do want to use Vector.
82
82
00:03:34,410 --> 00:03:36,600
Well, Vector is backed by an array just
83
83
00:03:36,600 --> 00:03:41,363
like ArrayList is and so let's go back to the IDE now.
84
84
00:03:44,870 --> 00:03:46,580
And I've created a project
85
85
00:03:46,580 --> 00:03:49,290
and I've added the same Employee class we had
86
86
00:03:49,290 --> 00:03:51,690
in the ArrayList video,
87
87
00:03:51,690 --> 00:03:53,360
same fields, same everything,
88
88
00:03:53,360 --> 00:03:54,520
I basically just copied
89
89
00:03:54,520 --> 00:03:56,200
and pasted it from that project.
90
90
00:03:56,200 --> 00:03:59,030
And right now I have this using an ArrayList.
91
91
00:03:59,030 --> 00:04:00,700
Now, if you want to use Vector,
92
92
00:04:00,700 --> 00:04:02,990
because we are assigning
93
93
00:04:02,990 --> 00:04:06,430
this into a list Employee instance rather
94
94
00:04:06,430 --> 00:04:07,890
than ArrayList Employee,
95
95
00:04:07,890 --> 00:04:10,713
all we have to do is change this to Vector,
96
96
00:04:12,900 --> 00:04:15,230
because Vector also implements list
97
97
00:04:15,230 --> 00:04:18,310
and so that's why rather than using
98
98
00:04:18,310 --> 00:04:21,670
a specific implementation in the declaration,
99
99
00:04:21,670 --> 00:04:24,370
it's always better to use the interface
100
100
00:04:24,370 --> 00:04:27,010
that the implementation is using
101
101
00:04:27,010 --> 00:04:30,380
because then you can switch out to another class
102
102
00:04:30,380 --> 00:04:32,160
that implements the same interface
103
103
00:04:32,160 --> 00:04:34,000
without having to change any code.
104
104
00:04:34,000 --> 00:04:37,600
So let's take a look at the Vector constructor.
105
105
00:04:37,600 --> 00:04:41,680
Let's go to the declaration, and let's go to this.
106
106
00:04:41,680 --> 00:04:43,870
We'll have to follow this down until we finally get
107
107
00:04:43,870 --> 00:04:46,980
to the actual code where it's doing the work.
108
108
00:04:46,980 --> 00:04:48,063
We'll keep going.
109
109
00:04:52,150 --> 00:04:56,030
And here we are and we'll see that an array is created.
110
110
00:04:56,030 --> 00:05:00,980
So it's using backing array just like ArrayList is.
111
111
00:05:00,980 --> 00:05:03,460
If we go back to Main now and we take a
112
112
00:05:03,460 --> 00:05:06,953
look at the implementation of add,
113
113
00:05:11,490 --> 00:05:16,490
and this time I want it for Vector, there you go.
114
114
00:05:17,180 --> 00:05:20,200
It's synchronised, and so
115
115
00:05:20,200 --> 00:05:24,200
that's the difference between the add method for ArrayList
116
116
00:05:24,200 --> 00:05:25,760
and the add method for Vector.
117
117
00:05:25,760 --> 00:05:28,460
Let's have a look at the ArrayList add method.
118
118
00:05:28,460 --> 00:05:30,830
I'll go to the other project,
119
119
00:05:30,830 --> 00:05:33,487
the other IntelliJ project for ArrayList.
120
120
00:05:36,800 --> 00:05:39,010
So here we are back in the project we worked with
121
121
00:05:39,010 --> 00:05:42,033
in the last video and if I go to this add method,
122
122
00:05:48,791 --> 00:05:49,624
I want the ArrayList add,
123
123
00:05:49,624 --> 00:05:52,610
we'll see that there is no synchronised keyword here,
124
124
00:05:52,610 --> 00:05:54,010
and so I want to reiterate
125
125
00:05:54,010 --> 00:05:57,080
that if you don't need thread safety, then don't use Vector.
126
126
00:05:57,080 --> 00:06:00,350
You don't want the overhead that comes with synchronisation,
127
127
00:06:00,350 --> 00:06:02,130
and if you do need thread safety,
128
128
00:06:02,130 --> 00:06:05,100
then you want to use Vector rather than using ArrayList
129
129
00:06:05,100 --> 00:06:07,790
because if you use ArrayList,
130
130
00:06:07,790 --> 00:06:10,290
then you're going to be responsible for synchronising
131
131
00:06:10,290 --> 00:06:13,890
all the calls to ArrayList methods,
132
132
00:06:13,890 --> 00:06:17,010
and so why do the work when the Vector class
133
133
00:06:17,010 --> 00:06:18,710
has already done all of that for you?
134
134
00:06:18,710 --> 00:06:19,543
And that's it.
135
135
00:06:19,543 --> 00:06:20,930
That's all I'm gonna say about Vector
136
136
00:06:20,930 --> 00:06:22,230
because it's pretty much the same
137
137
00:06:22,230 --> 00:06:24,700
as an ArrayList except that it's synchronised.
138
138
00:06:24,700 --> 00:06:27,700
The last two videos about ArrayList and Vector though,
139
139
00:06:27,700 --> 00:06:31,110
they're probably been review for some of you.
140
140
00:06:31,110 --> 00:06:32,440
I just want to make sure
141
141
00:06:32,440 --> 00:06:36,080
that we covered the two most popular classes
142
142
00:06:36,080 --> 00:06:37,580
in the JDK when it comes
143
143
00:06:37,580 --> 00:06:41,090
to just having a simple sequential list,
144
144
00:06:41,090 --> 00:06:43,810
so in the next video, we're going to move on to another type
145
145
00:06:43,810 --> 00:06:46,644
of list called a LinkedList.
146
146
00:06:46,644 --> 00:06:47,594
I'll see you there.
12512
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.