WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Intermediate · question 37 of 100

What is the difference between a Vector and an ArrayList in Java Collections?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

In Java, Vector and ArrayList are both classes that implement the List interface, but they have some differences in their implementation.

The main differences between Vector and ArrayList are:

Thread-safety: Vector is synchronized, which means that all its methods are thread-safe and can be accessed by multiple threads simultaneously without causing data corruption or race conditions. In contrast, ArrayList is not synchronized by default, and accessing it from multiple threads can cause concurrency issues unless proper synchronization mechanisms are in place.

Growth policy: Vector and ArrayList have different growth policies. When a Vector is full, its internal array is automatically resized by allocating a new array with twice the capacity of the previous one. In contrast, when an ArrayList is full, its internal array is resized by allocating a new array with 50% more capacity than the current one.

Performance: ArrayList tends to perform better than Vector in most cases, due to its unsynchronized nature and more efficient growth policy. However, this can vary depending on the use case and the specific JVM implementation being used.

Here’s an example code that demonstrates the differences between Vector and ArrayList:

import java.util.ArrayList;
import java.util.Vector;

public class Example {
    public static void main(String[] args) {
        Vector<Integer> vector = new Vector<>();
    
        // Add elements to the vector
        for (int i = 0; i < 10; i++) {
            vector.add(i);
        }
    
        ArrayList<Integer> arrayList = new ArrayList<>();
    
        // Add elements to the array list
        for (int i = 0; i < 10; i++) {
            arrayList.add(i);
        }
    
        // Access elements of the vector
        System.out.println("Vector elements:");
        for (int i = 0; i < vector.size(); i++) {
            System.out.println(vector.get(i));
        }
    
        // Access elements of the array list
        System.out.println("ArrayList elements:");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
    }
}

In this example, we create a Vector and an ArrayList and add 10 elements to each of them. We then iterate through the elements of each list and print them out.

Note that the Vector class is thread-safe, which makes it a good choice for use cases where multiple threads will be accessing the list concurrently. However, this thread safety comes at a performance cost. If thread-safety is not needed, ArrayList is generally preferred due to its better performance.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics