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

Java Collections · Expert · question 78 of 100

What is the difference between a Vector and a synchronized ArrayList in terms of usage and performance?

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

The Vector and synchronized ArrayList are two implementations of the List interface in Java Collections that provide thread-safety for concurrent access. However, there are some differences between them in terms of usage and performance.

The main difference between Vector and synchronized ArrayList is their synchronization mechanism. Vector uses synchronized keyword for every public method which makes it thread-safe, but it also causes performance overhead. On the other hand, synchronized ArrayList uses an explicit lock to synchronize its access which provides better performance than Vector.

Another difference between Vector and synchronized ArrayList is their growth rate. Vector doubles its size when it needs to expand, which results in more memory usage and slower performance. Synchronized ArrayList increases its size by 50% of its current capacity, which results in less memory usage and better performance.

Furthermore, Vector is an older implementation of List interface and is generally considered to be outdated. It is recommended to use synchronized ArrayList or other modern concurrent collections such as CopyOnWriteArrayList or ConcurrentLinkedDeque instead of Vector.

Here’s an example code that demonstrates the usage of Vector and synchronized ArrayList:

import java.util.List;
import java.util.Vector;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        // Creating a synchronized ArrayList
        List<String> synchronizedList = Collections.synchronizedList(new ArrayList<String>());

        // Adding elements to the synchronized list
        synchronizedList.add("apple");
        synchronizedList.add("banana");
        synchronizedList.add("orange");

        // Creating a Vector
        Vector<String> vector = new Vector<String>();

        // Adding elements to the vector
        vector.add("apple");
        vector.add("banana");
        vector.add("orange");

        // Accessing elements of the synchronized list
        synchronized (synchronizedList) {
            for (String element : synchronizedList) {
                System.out.println(element);
            }
        }

        // Accessing elements of the vector
        for (String element : vector) {
            System.out.println(element);
        }
    }
}

In this example, we create a synchronized ArrayList using the Collections.synchronizedList() method, and a Vector using the Vector() constructor. We add elements to both collections and then access them using for-each loops. We use the synchronized keyword for the synchronized list to ensure thread-safety, while the Vector is thread-safe by default.

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