Java Container Objects: Vector and ArrayList



Both Vector and ArrayList implement the List interface, and each of them uses (dynamically resizable) arrays for their internal data structure, similar to using an ordinary array.

However, there are many differences between ArrayList and Vector classes hence by reading this article, you will learn what ArrayList and Vector class are and their major difference that helps you to choose which one to opt for.

Understanding ArrayList and Vector Class

In addition to the Arrays class, Java provides an ArrayList class that can be used to create containers that stores lists of objects.

ArrayList is considered to be a growable array, and it gives you fast iteration and fast random access.

The syntax for the ArrayList is as follows ?

ArrayList: ArrayList<T> al = new ArrayList<T>();

Example

Here's an example illustrating ArrayList to store and traverse the elements

Open Compiler
import java.util.*; public class TestArrayList21{ public static void main(String args[]){ List<String> al=new ArrayList<String>(); //creating arraylist al.add("Mike"); //adding object in arraylist al.add("Jack"); al.add("Rose"); al.add("James"); //traversing elements using Iterator Iterator itr=al.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }

When executing the above program, it gives the following result.

Output

Mike
Jack
Rose
James

On the other hand, earlier versions of Java have one legacy collection class called Vector, which is very much similar to ArrayList and implements a dynamic array.

The syntax for the ArrayList is as follows ?

Vector: Vector<T> v = new Vector<T>();

Example

Here's an example illustrating the Java Vector class that uses the Enumeration interface

Open Compiler
import java.util.*; public class TestVector1{ public static void main(String args[]){ Vector<String> v=new Vector<String>(); //creating vector v.add("Stephanie"); //method of Collection v.addElement("Amith"); //method of Vector v.addElement("Andros"); //traversing elements using Enumeration Enumeration e=v.elements(); while(e.hasMoreElements()){ System.out.println(e.nextElement()); } } }

When executing the above program, it gives the following result.

Output

Stephanie
Amith
Andros

Difference between ArrayList and Vector

Based on the following factors, both the Array list and Vector class have certain differences, which are listed below.

Synchronization

Vector is synchronized, so only one thread can access the code at a time, while ArrayList is not synchronized, so multiple threads can work on ArrayList at the same time.

For example, in a multithreading environment, if one thread is performing an add operation, another thread may be performing a remove operation. In the case of multiple threads accessing ArrayList simultaneously, then we need to synchronize the block of code that modifies the list structurally or allow simple element modifications. A structural modification is the addition or removal of an element from a list. Changing the value of an existing element is not a structural modification.

Performance

Since ArrayList is non-synchronized, it is faster, whereas vector operations give slower performance as they are synchronized (thread-safe), so when one thread works on a vector, it acquires a lock, which forces other threads to wait until the lock is released before working on it.

Data Growth

To maintain optimal use of storage, both ArrayList and Vector grow and shrink dynamically - but the way they resize is totally a different one.

Generally, ArrayList increments 50% of the current array size if the total number of elements exceeds its capacity, whereas vector increments 100% - essentially doubling the current array size if the total number of elements exceeds its capacity.

Traversal

To traverse over vector elements, generally, Vector uses both Enumeration and Iterator, whereas ArrayList will only use Iterator.

Applications

Most programmers prefer ArrayList over Vector because ArrayList can be synchronized explicitly with Collections.

Note? When there is no specific requirement to use vector, ArrayList is most often preferred by the developers.

Example

Here's the Java Program illustrating the use of ArrayList and Vector in Java ?

Open Compiler
import java.io.*; import java.util.*; public class Test{ public static void main (String[] args){ // creating an ArrayList ArrayList<String> al = new ArrayList<String>(); // adding object to arraylist al.add("Learn.tutorialspoint.com"); al.add("Coding.tutorialspoint.com"); al.add("Practice.tutorialspoint.com"); al.add("Test.tutorialspoint.com"); // traversing elements using Iterator' System.out.println("ArrayList elements are:"); Iterator it = al.iterator(); while (it.hasNext()) System.out.println(it.next()); // creating Vector Vector<String> v = new Vector<String>(); v.addElement("Learn"); v.addElement("Practice"); v.addElement("Coding"); // traversing elements using Enumeration System.out.println("\nVector elements are:"); Enumeration e = v.elements(); while (e.hasMoreElements()) System.out.println(e.nextElement()); } }

When running the above program, it produces the below output

Output

ArrayList elements are:
Learn.tutorialspoint.com
Coding.tutorialspoint.com
Practice.tutorialspoint.com
Test.tutorialspoint.com
Vector elements are:
Learn
Practice
Coding

Wrapping up

From the above discussion, we hope you've better understood what java container objects like ArrayList and Vector are. Here's a summary of what we discussed above.

  • ArrayList is unsynchronized and not thread-safe, whereas Vectors are synchronized.

  • A Vector can only be called by one thread, which is a little overhead but helpful when safety is a major concern. Therefore, in a single-threaded case, ArrayList is most preferable, whereas multithreading is concerned, vectors are the most obvious choice.

  • If we are not sure about the amount of data we will have but are aware of the rate at which it grows, Vector has an advantage because we can set the increment value in vectors. Vector increments 100%, which means doubles the array size if the total number of elements exceeds its capacity.

  • ArrayList is newer and faster. So, if you don't have any clear requirements for using either of them, you can use ArrayList over vector.

We hope you find this article helpful. If you like this article, please share it with your colleagues, and if you have any other doubts, please comment below.

Updated on: 2022-10-13T11:43:17+05:30

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements