Java Vector vs ArrayList

In Java, ArrayList and Vector, both implement 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. Difference between 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 a vector needs to increase capacity to add an element (when existing capacity is filled), Vector increases the capacity by 100% during resizing. 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.

The 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. Converting Vector to ArrayList

To convert a vector containing objects to an ArrayList containing similar objects, we can use the ArrayList constructor, which accepts another collection and initialize the ArrayList with the elements of the vector.

Vector<String> vector = new Vector<>();
 
vector.add("A");
vector.add("B");
vector.add("C");
vector.add("D");
 
ArrayList<String> arrayList = new ArrayList<>(vector);    //[A, B, C, D]

3. Converting ArrayList to Vector

The conversion from ArrayList to vector is very similar to the previous example. Here we have to use the Vector constructor. It accepts another collection and initializes the vector with the elements of ArrayList.

ArrayList<String> arrayList = new ArrayList<>();
         
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
 
Vector<String> vector = new Vector<>(arrayList);

4. 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

Comments

Subscribe
Notify of
guest
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.