Difference between ArrayList and Vector in Java

ArrayList and Vector, both implements java.util.List interface and provide the capability to store and get objects within using simple API methods. Still, they are different in many aspects, and we need to understand both classes in detail to make a wise decision about when to use which class.

1. ArrayList vs. Vector

1.1. Thread Safety

Vector is a synchronized collection and ArrayList is not. It simply means that when working on concurrent applications, we can use Vector without any additional synchronization control implemented by the developer using synchronized keyword. Public methods inside the Vector are defined as synchronized, which makes all operations in the Vector safe for concurrency needs.

To use ArrayList in concurrent applications, we must explicitly control the thread access to the instance to make the application work as intended. If we want to get a synchronized version of arraylist, then we can use Collections.synchronizedList() method.

The returned list is an internal implementation of List interface different from arraylist. But it has the same capability as the arraylist class.

ArrayList<String> namesList = new ArrayList<>();
 
namesList.add("alex");
namesList.add("brian");
namesList.add("charles");
namesList.add("david");
 
//Synchronized list
List<String> syncNamesList = Collections.synchronizedList(namesList);
 
syncNamesList.add("edwin");
 
System.out.println(syncNamesList);

1.2. Capacity Increment

By default, when vector needs to increase capacity to add an element (when existing capacity is filled), it increases the capacity by 100%. It means vector size grows to double of previous capacity. We can override the default capacity using the constructor Vector(int initialCapacity, int capacityIncrement). Here the second argument is the amount by which the capacity is increased when the vector overflows.

In ArrayList, by default, capacity grows by 50% of the existing capacity. In the arraylist, we can define the initial capacity but not the capacity increment factor.

1.3. Performance

Both collections have a backing array on which they store and search elements. So essentially, there is not much performance difference in add-and-get operations.

Real performance difference comes when we take synchronization into consideration. ArrayList is non-synchronized, so there is no time lapse in thread safety. Whereas Vector is synchronized, so it has some overhead in thread management/ locking etc.

1.4. ConcurrentModificationException

There is one difference in how these collections handle the iteration while the collection is still modified by the program.

ArrayList provide fail-fast iterators. As soon as we modify the arraylist structure (add or remove elements), the iterator will throw ConcurrentModificationException error.

Vector provide iterator as well as enumeration. Iterators are fail-fast but enumerations are not. If we modify the vector during iteration over enumeration, it does not fail.

//Vector
Vector<String> vector = new Vector<>(Arrays.asList("A","B","C"));
 
Enumeration<String> vectorEnums = vector.elements();
 
while(vectorEnums.hasMoreElements()) {
  String value = vectorEnums.nextElement();
  if("B".equals(value)) {
    vector.add("D");
  }
  System.out.println(value);
}
 
System.out.println("================");
     
//ArrayList
ArrayList<String> list = new ArrayList<>(Arrays.asList("A","B","C"));
 
Iterator<String> listItr = list.iterator();
 
while(listItr.hasNext()) {
  String value = listItr.next();
  if("B".equals(value)) {
    list.add("D");
  }
  System.out.println(value);
}

Program output.

A
B
C
D

================

A
B
Exception in thread "main"
java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at com.howtodoinjava.ArrayListExample.main(ArrayListExample.java:33)

2. Conclusion

In this short Java tutorial, we compared the Vector with ArrayList. Note that as of the Java 2 platform v1.2, Vector class was retrofitted to implement the List interface. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.

Happy Learning !!

Read More:

ArrayList Java Docs
Vector Java Docs

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial