Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
(upbeat music) 2
2
Well, this is gonna be a short video 3
3
because the Vector class 4
4
is essentially a thread safe ArrayList, 5
5
but Vector actually came first and we're gonna take a 6
6
look at the Vector Javadoc and we'll see, 7
7
if we scroll down a bit 8
8
that Vector has been there since the beginning. 9
9
It has been in the JDK since JDK 1.0. 10
10
If we come up to the top here, 11
11
now, if I go back to ArrayList, 12
12
so this is the ArrayList class, 13
13
we'll see that that's, there was only added into 14
14
the JDK in JDK version 1.2. 15
15
Now, if you've been using Java as long as I have, 16
16
you might remember when ArrayList was introduced. 17
17
Before that, the list go-to class was Vector, 18
18
and so the question is if Vector was in JDK version 1.0 19
19
and it has pretty much the same methods 20
20
as the ArrayList class, 21
21
why was ArrayList added to the JDK? 22
22
Well, the answer lies in the documentation. 23
23
If we scroll back up, we'll see right here 24
24
in big bold letters for ArrayList, 25
25
this implementation is not synchronised, 26
26
and if we go back to the Vector class, 27
27
it says unlike the new collection implementations, 28
28
Vector is synchronised. 29
29
So Vector is thread safe meaning it's okay 30
30
to use it from different threads without you having 31
31
to synchronise the code. 32
32
But ArrayList is not. 33
33
So if you only read an ArrayList, 34
34
then it's safe to use it from multiple threads, 35
35
no data will be clobbered, 36
36
but if you use ArrayList with multiple threads 37
37
and one or more of those threads is writing 38
38
to the ArrayList by adding, 39
39
deleting, setting or changing objects in the list, 40
40
then you could run into a conflict. 41
41
You could have threading conflicts, 42
42
and so in that case, you'd 43
43
want to use Vector rather than ArrayList. 44
44
So if you need thread safety, use Vector. 45
45
If you don't, use ArrayList. 46
46
Now, the reason that ArrayList came along 47
47
is that synchronisation has an overhead involved. 48
48
So it slows things down, 49
49
and so when only the Vector class was available 50
50
and Vector is synchronised, 51
51
if you wanted to use a list backed by an array 52
52
and you wanted to use the class in the JDK, 53
53
you were pretty much forced 54
54
to use a synchronised class even 55
55
if you didn't need synchronisation, 56
56
and so because of that, 57
57
ArrayList was eventually added to the JDK, 58
58
so that if you don't need synchronisation, 59
59
you would have a class backed by an array 60
60
that you could use that doesn't 61
61
have the overhead involved with synchronisation, 62
62
and so that's why ArrayList came along. 63
63
It was because developers were saying, 64
64
hey, we like Vector, but what do we do 65
65
if we don't need synchronisation? 66
66
We don't want the overhead associated with it, 67
67
and so a couple of versions later, ArrayList came along. 68
68
And so here you'll see that the documentation 69
69
for ArrayList itself says 70
70
if a thread safe implementation is not needed, 71
71
it is recommended to use ArrayList. 72
72
So you only want to use the Vector class 73
73
if you need synchronisation. 74
74
If you don't need it and you use the Vector class, 75
75
you're going to be using a slower implementation 76
76
for no reason, and that's why you 77
77
want to use ArrayList unless you need 78
78
a thread safe implementation. 79
79
So if you need thread safety, 80
80
use Vector, if you don't, use ArrayList. 81
81
Okay, so let's say we do want to use Vector. 82
82
Well, Vector is backed by an array just 83
83
like ArrayList is and so let's go back to the IDE now. 84
84
And I've created a project 85
85
and I've added the same Employee class we had 86
86
in the ArrayList video, 87
87
same fields, same everything, 88
88
I basically just copied 89
89
and pasted it from that project. 90
90
And right now I have this using an ArrayList. 91
91
Now, if you want to use Vector, 92
92
because we are assigning 93
93
this into a list Employee instance rather 94
94
than ArrayList Employee, 95
95
all we have to do is change this to Vector, 96
96
because Vector also implements list 97
97
and so that's why rather than using 98
98
a specific implementation in the declaration, 99
99
it's always better to use the interface 100
100
that the implementation is using 101
101
because then you can switch out to another class 102
102
that implements the same interface 103
103
without having to change any code. 104
104
So let's take a look at the Vector constructor. 105
105
Let's go to the declaration, and let's go to this. 106
106
We'll have to follow this down until we finally get 107
107
to the actual code where it's doing the work. 108
108
We'll keep going. 109
109
And here we are and we'll see that an array is created. 110
110
So it's using backing array just like ArrayList is. 111
111
If we go back to Main now and we take a 112
112
look at the implementation of add, 113
113
and this time I want it for Vector, there you go. 114
114
It's synchronised, and so 115
115
that's the difference between the add method for ArrayList 116
116
and the add method for Vector. 117
117
Let's have a look at the ArrayList add method. 118
118
I'll go to the other project, 119
119
the other IntelliJ project for ArrayList. 120
120
So here we are back in the project we worked with 121
121
in the last video and if I go to this add method, 122
122
I want the ArrayList add, 123
123
we'll see that there is no synchronised keyword here, 124
124
and so I want to reiterate 125
125
that if you don't need thread safety, then don't use Vector. 126
126
You don't want the overhead that comes with synchronisation, 127
127
and if you do need thread safety, 128
128
then you want to use Vector rather than using ArrayList 129
129
because if you use ArrayList, 130
130
then you're going to be responsible for synchronising 131
131
all the calls to ArrayList methods, 132
132
and so why do the work when the Vector class 133
133
has already done all of that for you? 134
134
And that's it. 135
135
That's all I'm gonna say about Vector 136
136
because it's pretty much the same 137
137
as an ArrayList except that it's synchronised. 138
138
The last two videos about ArrayList and Vector though, 139
139
they're probably been review for some of you. 140
140
I just want to make sure 141
141
that we covered the two most popular classes 142
142
in the JDK when it comes 143
143
to just having a simple sequential list, 144
144
so in the next video, we're going to move on to another type 145
145
of list called a LinkedList. 146
146
I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.