Differences between ArrayList and Vector in Java
In this post, we will compare and contrast ArrayList and Vector in Java, and explain their advantages and disadvantages.
1. Overview of ArrayList
ArrayList is a class that implements the List interface and uses a dynamically resizable array as its underlying data structure to store elements. ArrayList is one of the most commonly used classes in Java, and it offers many benefits over other types of lists. Here is an example of how to create and use an ArrayList in Java:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { // Create an ArrayList object List<String> list = new ArrayList<>(); // Add some elements to the list list.add("Java"); list.add("Python"); list.add("C++"); System.out.println(list); // [Java, Python, C++] // Modify the list list.add(1, "Ruby"); // Add a new element at a specific index list.set(2, "JavaScript"); // Replace an existing element with a new element list.remove(3); // Remove an element from a specific index // Print the updated list System.out.println(list); // [Java, Ruby, JavaScript] // Iterate over the list using a for-each loop for (String s : list) { System.out.println(s); } // Java // Ruby // JavaScript } } |
2. Overview of Vector
Vector is a class that implements the List interface and uses a dynamically resizable array as its underlying data structure to store elements. Vector is one of the oldest classes in Java and has been available since Java 1.0. Here is an example of how to create and use a Vector in Java:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.util.List; import java.util.Vector; class Main { public static void main(String[] args) { // Create a Vector object List<String> v = new Vector<>(); // Add some elements to the list v.add("Java"); v.add("Python"); v.add("C++"); System.out.println(v); // [Java, Python, C++] // Modify the list v.add(1, "Ruby"); // Add a new element at a specific index v.set(2, "JavaScript"); // Replace an existing element with a new element v.remove(3); // Remove an element from a specific index // Print the updated list System.out.println(v); // [Java, Ruby, JavaScript] // Iterate over the list using a synchronized block and a for-each loop synchronized (v) { for (String s : v) { System.out.println(s); } } // Java // Ruby // JavaScript } } |
2. Similarities between ArrayList and Vector
Both ArrayList and Vector are resizable-array implementations of the List interface. ArrayList is roughly equivalent to Vector and have many similarities:
- Both classes are members of the Java Collections Framework and implements the
Listinterface. - Both classes internally use an array data structure to store the list.
- Both classes can automatically grow or shrink to accommodate new items and remove existing elements.
- Both classes maintain the insertion order of elements, i.e., they are ordered.
- The iterators returned by
iterator()andlistIterator()methods ofArrayListandVectorare fail-fast. - Both classes permits null values and duplicates.
3. Differences between ArrayList and Vector
Now let’s discuss some of the major differences between the ArrayList and Vector implementations.
1. Synchronization
The primary difference between an ArrayList and Vector is that a Vector implementation is synchronized while an ArrayList implementation is not synchronized. This means that only a single thread can operate on a Vector method at a time, while multiple threads can operate on an ArrayList concurrently.
The ArrayList is not suitable for multithreading environments where thread safety and consistency are required. If multiple threads access ArrayList concurrently, it may lead to data corruption, inconsistency, or unexpected behavior. To make an ArrayList thread-safe, it can be synchronized externally using the Collections.synchronizedList() method.
A Vector is synchronized by default, but is not exactly thread-safe. This is because Vector synchronizes on each operation and not the whole Vector instance itself.
2. Performance
Vectors are very slow as they are synchronized, and a single thread can obtain a lock on an operation, making other threads wait until that lock is released. ArrayList, on the other hand, is much faster than a Vector as it is not synchronized and multiple threads can operate on it at the same time. This makes it fast and efficient, as it does not incur any overhead or performance penalty due to locking or blocking.
3. Storage management
Both ArrayList and Vector can grow and shrink dynamically to accommodate new elements if needed. Instead of incremental reallocation, the storage increases in chunks. Normally when new elements are added and the capacity is full, an ArrayList increases its size by half of its current size, and a Vector doubles its size. All this is taken care of automatically by the Java Virtual Machine (JVM). However, this does not affect the logical order of the elements in the list, which is determined by their index.
4. Fail-fast
For traversing the list, an ArrayList uses an iterator while a Vector uses both enumeration and iterator. The Enumeration returned by the vector method is not fail-fast. In contrast, the iterators returned by the iterator() and listIterator() methods of both Vector and ArrayList are fail-fast and throws ConcurrentModificationException if the collection is structurally modified after the iterator is created except through the iterator’s own remove() or add() methods.
4. Which implementation to use?
An ArrayList should always be preferred over a Vector. A Vector is a legacy class which deprecated and should be avoided at all costs. The Vector class was not included as part of the Java Collection Framework and was included later. We have already mentioned that a Vector is synchronized but not completely thread-safe. Vector also has the overhead of locking whether synchronization is required or not.
If a thread-safe implementation of List interface is required, we can either use CopyOnWriteArrayList class, which is a thread-safe variant of the ArrayList or synchronize ArrayList externally using the Collections.synchronizedList() method.
That’s all about the differences between ArrayList and Vector in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)