What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?
-
6I am not seeing the exact duplicate here. – Jeff Atwood Jun 07 '10 at 08:15
-
2Well, you can create vectors in java as well - `Vector v = new Vector(3, 2);` – sgsi Jan 20 '15 at 16:44
-
Never use `Vector`, use `ArrayList` or `LinkedList` or `ArrayDeque` – Feb 24 '18 at 01:18
7 Answers
Differences
- Vectors are synchronized, ArrayLists are not.
- Data Growth Methods
Use ArrayLists if there is no specific requirement to use Vectors.
Synchronization
If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.
Collections.synchronizedList
is normally used at the time of creation of the list to avoid any accidental unsynchronized access to the list.
Data growth
Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

- 37,270
- 24
- 156
- 208

- 15,401
- 9
- 56
- 75
-
9@Rei Exactly what he said: _Multiple threads_ (https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html) – RecursiveExceptionException Aug 08 '16 at 02:10
-
1What about READING from an ArrayList in a multithreaded way? Is that thread-safe? – Xunie Oct 20 '16 at 19:12
-
2@Xunie Reading from ArrayList or other collection classes never be a problem. The problem arises when you are adding or removing or modifying existing values to the ArrayList or the collection. – sainath reddy Oct 23 '16 at 10:27
-
The reference link given here at the bottom of the answer is stale and leads to spam. – Gonen I May 20 '21 at 05:51
-
Can any one justify the name Vector? Because in Physics Vector has a different meaning altogether. But in mathematics they say vector is a row of data. – Sreekanth Karumanaghat Sep 02 '21 at 17:18
-
Using vector here is almost 99% because C++'s arraylist is called a vector. As for that, you could check this thread. https://stackoverflow.com/questions/581426/why-is-a-c-vector-called-a-vector – EricChen1248 Nov 15 '21 at 01:30
As the documentation says, a Vector
and an ArrayList
are almost equivalent. The difference is that access to a Vector
is synchronized, whereas access to an ArrayList
is not. What this means is that only one thread can call methods on a Vector
at a time, and there's a slight overhead in acquiring the lock; if you use an ArrayList
, this isn't the case. Generally, you'll want to use an ArrayList
; in the single-threaded case it's a better choice, and in the multi-threaded case, you get better control over locking. Want to allow concurrent reads? Fine. Want to perform one synchronization for a batch of ten writes? Also fine. It does require a little more care on your end, but it's likely what you want. Also note that if you have an ArrayList, you can use the Collections.synchronizedList
function to create a synchronized list, thus getting you the equivalent of a Vector
.

- 36,191
- 7
- 77
- 140
Vector
is a broken class that is not threadsafe, despite it being "synchronized" and is only used by students and other inexperienced programmers.
ArrayList
is the go-to List implementation used by professionals and experienced programmers.
Professionals wanting a threadsafe List implementation use a CopyOnWriteArrayList
.
-
18synchronized but not threadsafe? what does it mean? [i'm beginner] – Dineshkumar May 25 '13 at 08:50
-
18@Dineshkumar `Vector` was *intended* to be threadsafe, but has a design flaw that makes it *not* in fact threadsafe, It is basically a deprecated class. For some reason, universities etc haven't heard about this news and still advocate its use. – Bohemian May 25 '13 at 09:28
-
2@Dineshkumar see [this quesion](http://stackoverflow.com/questions/2144851/synchronization-in-vectors-in-java) – Bohemian May 25 '13 at 09:52
-
fine. but what the flaw you are saying that its not threadsafe. (thats the reasion its deprecated?) – Dineshkumar May 25 '13 at 09:59
-
5@Dineshkumar sorry - that wasn't a good link. here's the [definitive answer](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated). In short, its synchronisation is useless. – Bohemian May 25 '13 at 10:40
-
It is not useless: 1. It makes sure that multi-thread updating the vector is not going to mess up the internal of the list. 2. If you need to synchronize multiple operation you always need to explicit synchronize and do the operations. In case of ArrayList, if you synchronize on "multi operations" but not single ones, the internal can be messed up. CopyOnWriteArrayList is solving only specific problem which is not to replace every use of synchronized list. (Anyway, I will recommend `Collections.synchronizedList(new ArrayList())` instead of Vector for synchronized list) – Adrian Shum Nov 20 '13 at 10:30
-
2@AdrianShum `Collections.synchronizedList()` is inefficient and all but deprecated. As per the javadoc, clients must manually synchronize on the returned list to iterate safely: Although threadsafe, multiple threads can not safely concurrently iterate, making it a brutal, unscaleable solution. Its use has been superseded by the `java.util.concurrent` classes – Bohemian Nov 20 '13 at 13:17
-
10
ArrayList
is newer and 20-30% faster.
If you don't need something explitly apparent in Vector
, use ArrayList

- 235,628
- 64
- 220
- 299
-
37
-
6@user At the time it was just personal experience from clunking aroun huge arrays. Over three years on now, I can't point you to exactly what I was talking about but there are plenty of benchmarks out there. It's not until threading where you see the biggest jumps but here's one: http://www.javacodegeeks.com/2010/08/java-best-practices-vector-arraylist.html – Oli Aug 27 '13 at 09:58
-
1The 20-30% only match, if you read AND write to the Vector/Arraylist, since the growth function will make the biggest impact. If you have a benchmark that writes only once and then performs reads only will deliver a different result – Tobi Dec 08 '13 at 16:25
-
3
-
1Since the vector is synchronised and arraylist is not synchronised, that may be a reason, arraylist are faster than vector. – ASK Sep 16 '19 at 12:11
There are 2 major differentiation's between Vector and ArrayList.
Vector is synchronized by default, and ArrayList is not. Note : you can make ArrayList also synchronized by passing arraylist object to Collections.synchronizedList() method. Synchronized means : it can be used with multiple threads with out any side effect.
ArrayLists grow by 50% of the previous size when space is not sufficient for new element, where as Vector will grow by 100% of the previous size when there is no space for new incoming element.
Other than this, there are some practical differences between them, in terms of programming effort:
- To get the element at a particular location from Vector we use elementAt(int index) function. This function name is very lengthy. In place of this in ArrayList we have get(int index) which is very easy to remember and to use.
- Similarly to replace an existing element with a new element in Vector we use setElementAt() method, which is again very lengthy and may irritate the programmer to use repeatedly. In place of this ArrayList has add(int index, object) method which is easy to use and remember. Like this they have more programmer friendly and easy to use function names in ArrayList.
When to use which one?
- Try to avoid using Vectors completely. ArrayLists can do everything what a Vector can do. More over ArrayLists are by default not synchronized. If you want, you can synchronize it when ever you need by using Collections util class.
- ArrayList has easy to remember and use function names.
Note : even though arraylist grows by 100%, you can avoid this by ensurecapacity() method to make sure that you are allocating sufficient memory at the initial stages itself.
Hope it helps.

- 4,684
- 35
- 29
-
5Wrong info (switched) on grow size for ArrayLIst and Vector, otherwise quite good answer. – Nenad Bulatović Jan 19 '15 at 01:27
-
1The grow of Vector is doubling with needed http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html while of ArrayList "The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost." http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html – Mohamed El-Nakeep Mar 04 '15 at 19:53
-
I don't understand how the method name can be a criteria for using or not using that method. – Surender Khairwa Nov 12 '19 at 04:53
ArrayList
and Vector
both implements List interface and maintains insertion order.But there are many differences between ArrayList
and Vector
classes...
ArrayList
is not synchronized.ArrayList
increments 50% of current array size if number of element exceeds from its capacity.ArrayList
is not a legacy class, it is introduced in JDK 1.2.ArrayList
is fast because it is non-synchronized.ArrayList
uses Iterator interface to traverse the elements.
Vector -
Vector
is synchronized.Vector
increments 100% means doubles the array size if total number of element exceeds than its capacity.Vector
is a legacy class.Vector
is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.Vector
uses Enumeration interface to traverse the elements. But it can use Iterator also.
See Also : https://www.javatpoint.com/difference-between-arraylist-and-vector

- 7,942
- 7
- 60
- 65
Basically both ArrayList and Vector both uses internal Object Array.
ArrayList: The ArrayList class extends AbstractList and implements the List interface and RandomAccess (marker interface). ArrayList supports dynamic arrays that can grow as needed. It gives us first iteration over elements. ArrayList uses internal Object Array; they are created with an default initial size of 10. When this size is exceeded, the collection is automatically increases to half of the default size that is 15.
Vector: Vector is similar to ArrayList but the differences are, it is synchronized and its default initial size is 10 and when the size exceeds its size increases to double of the original size that means the new size will be 20. Vector is the only class other than ArrayList to implement RandomAccess. Vector is having four constructors out of that one takes two parameters Vector(int initialCapacity, int capacityIncrement) capacityIncrement is the amount by which the capacity is increased when the vector overflows, so it have more control over the load factor.
Some other differences are:

- 4,629
- 8
- 37
- 52
-
1Why adding object at first and at the end in LinkedList is slow? Shouldn't it be faster than BOTH arrayList and vector? – CHANist Apr 22 '16 at 00:28
-
@CHANist I agree too. Adding an object at start and end should be faster than adding an object in the middle of it. – Rahul Rastogi Jul 19 '17 at 05:43
-
1The language used in the LinkedList column of this table is contradictory. Both prepending and appending to a LinkedList are faster than adding elements in the middle, but slower than prepending or appending to ArrayLists or Vectors. This is because every insertion requires memory allocation with non-local references, increasing the chance of cache misses. Even though lookup in a LinkedList is linear with the number of elements and a pointer to the end isn't stored, appending is still faster than prepending because memory is only reallocated for one element. – Sophia Gold Jul 30 '17 at 22:00