Difference between ArrayList and Vector in Java

Here you will get to know the difference between ArrayList and Vector in Java programming language.

ArrayList

It is a class in Java that implements List interface. Unlike traditional array data structure, it is not fixed in length and doesn’t require its size to be specified in order to declare it. They can grow and can be reduced dynamically as per need. Apart from these benefits ArrayList class has many pre-defined methods which could help the developers save a lot of time.

Also Read: Difference between Array and ArrayList in Java

Implementation in Java

Output

Currently the array list has following elements:[Jack, John, Steve, Joey]
Current array list is:[Ross, Monica, Jack, John, Steve, Joey]
Current array list is:[Ross, Monica, Jack, John, Joey]
Current array list is:[Ross, Jack, John, Joey]

Vector

Like ArrayList, Vector also implements List interface. It also supports dynamic size alteration. Vector is synchronized which means that any method which is used in Vector is thread safe i.e. only one thread can perform operations at one time on a vector. This can make it comparatively slower than ArrayList.

Implementation in Java

Output

Size is: 4
Default capacity increment is: 4
Size after addition: 7
Capacity after increment is: 8

Elements are:
Apple Orange Kiwi Mango fruit1 fruit2 fruit3

Difference between ArrayList and Vector in Java

ArrayList Vector
1. ArrayList is not thread-safe leading to being non-synchronized. So it gives better performance. Vector is thread-safe meaning its synchronized. So its performance is not good as ArrayList.
2. It uses Iterator interface to traverse elements. Vector can use both Enumeration and Iterator interface to traverse the elements.
3. If inserting an element into the ArrayList exceeds capacity then it increases its size by 50%. A Vector doubles its size if inserting an element exceeds its capacity.
4. ArrayList does not define the increment size. Vector can define the increment size using setSize() method.
5. It was introduced in java version 1.2 in Java Collections framework It was there in java since the first version of the java development kit (jdk).

Thus we learnt about difference between Vector and ArrayList in Java which are both very similar and different at the same time and allow the user to use them as per their needs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.