Java
Programming
Data Structures
ArrayList
Vector

What are the differences between ArrayList and Vector?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

ArrayList and Vector are both resizable-array implementations of the List interface, so they look similar at first glance. The practical differences are mostly about synchronization, API age, and what you should choose in modern Java code.

Similarities First

Both classes store elements in an indexed array that grows when needed. Both preserve insertion order, allow duplicates, and provide random access with get(index).

java
1import java.util.ArrayList;
2import java.util.List;
3import java.util.Vector;
4
5public class ListBasics {
6    public static void main(String[] args) {
7        List<String> arrayList = new ArrayList<>();
8        List<String> vector = new Vector<>();
9
10        arrayList.add("alpha");
11        arrayList.add("beta");
12
13        vector.add("alpha");
14        vector.add("beta");
15
16        System.out.println(arrayList.get(0));
17        System.out.println(vector.get(0));
18    }
19}

In single-threaded code, both examples behave the same way for basic operations. That is why many beginners wonder whether there is still any meaningful difference.

Synchronization Is the Main Behavioral Difference

Vector synchronizes its individual methods. ArrayList does not. That means Vector adds locking overhead to method calls such as add, get, and remove, while ArrayList is faster in the common case where one thread owns the list or external synchronization is already present.

java
1import java.util.ArrayList;
2import java.util.Collections;
3import java.util.List;
4
5public class SynchronizedListExample {
6    public static void main(String[] args) {
7        List<Integer> list = Collections.synchronizedList(new ArrayList<>());
8
9        list.add(10);
10        list.add(20);
11
12        synchronized (list) {
13            for (int value : list) {
14                System.out.println(value);
15            }
16        }
17    }
18}

This modern pattern is usually preferred over reaching for Vector automatically. It makes synchronization explicit and lets you choose the appropriate collection implementation first.

It is also important to remember that Vector being synchronized does not solve every concurrency problem. Compound operations such as "check then act" or iteration plus modification still need additional coordination.

API Age and Design Style

Vector is a legacy class from early Java. It is not deprecated, but it carries older naming conventions and compatibility features such as elements() returning an Enumeration. ArrayList was added later as part of the collections framework and fits modern Java style better.

java
1import java.util.Enumeration;
2import java.util.Vector;
3
4public class VectorEnumerationExample {
5    public static void main(String[] args) {
6        Vector<String> vector = new Vector<>();
7        vector.add("one");
8        vector.add("two");
9
10        Enumeration<String> items = vector.elements();
11        while (items.hasMoreElements()) {
12            System.out.println(items.nextElement());
13        }
14    }
15}

That code works, but in new programs you would normally write a for loop, an iterator, or a stream pipeline instead.

Capacity Growth and Performance Notes

Both lists grow automatically, but they do not necessarily use the same growth strategy. That matters much less than synchronization overhead and overall access patterns, but it does mean you should not assume identical resizing behavior.

For most real applications, the practical guidance is simple:

  • Use ArrayList for general-purpose list storage.
  • Use Collections.synchronizedList(new ArrayList<>()) if you need a synchronized wrapper.
  • Consider concurrent collections such as CopyOnWriteArrayList if your access pattern is specifically concurrent and read-heavy.

This advice is stronger than saying "Vector is slower." The better point is that Vector is usually not the collection you would choose first in a new design.

When Vector Still Appears

You may still encounter Vector in older codebases, older libraries, or interview questions. If you are maintaining such code, the class is still valid Java. There is no rule that says it must be removed immediately.

What matters is understanding the tradeoff. If the code does not actually rely on synchronized method calls, replacing Vector with ArrayList may simplify intent and remove unnecessary locking. If the code does rely on synchronization, then migration needs more thought because switching to ArrayList changes thread-safety expectations.

Common Pitfalls

  • Assuming Vector is automatically the right answer for multithreaded code.
  • Forgetting that iteration over a synchronized list still needs external synchronization.
  • Treating ArrayList and Vector as identical except for performance.
  • Using Vector in new code when a clearer modern collection choice exists.
  • Replacing Vector in legacy code without checking whether callers relied on synchronized methods.

Summary

  • 'ArrayList and Vector are both resizable indexed lists.'
  • 'Vector synchronizes individual methods, while ArrayList does not.'
  • 'ArrayList is the usual default choice in modern Java.'
  • If synchronization is needed, prefer an explicit wrapper or a concurrent collection chosen for the workload.
  • 'Vector still works, but it is mainly a legacy API rather than the first recommendation for new code.'

Course illustration
Course illustration

All Rights Reserved.