Here are the key points highlighting the differences between ArrayList and Vector in Java:
Synchronization:
- ArrayList: Not synchronized, meaning it’s not thread-safe by default.
- Vector: Synchronized, so it’s thread-safe, but this introduces an overhead that can impact performance.
Performance:
- ArrayList: Generally faster since it doesn’t have the overhead of synchronization.
- Vector: Slower compared to
ArrayList
due to its inherent synchronization.
Capacity Increment:
- ArrayList: When the elements exceed their capacity, they increase by 50% of their current size.
- Vector: By default, it doubles its array size. However, you can specify the increment value when the vector needs to grow.
Legacy:
- ArrayList: Introduced later, in Java 2, and is part of the Java Collections Framework (JCF).
- Vector: Older, present in the original version of Java. Considered legacy now, and its use is discouraged in new code.
Flexibility:
- ArrayList: More flexible, as you can make it synchronized if required using
Collections.synchronizedList()
. - Vector: Always synchronized.
Fail-fast Iterators:
- Both
ArrayList
andVector
provide fail-fast iterators. This means if one thread changes the structure of the list (by adding or removing elements) after an iterator is created by another thread, the iterator will throw aConcurrentModificationException
.
Null Elements:
- Both
ArrayList
andVector
allow multiple null elements.
Interfaces:
- Both
ArrayList
andVector
implement theList
interface, so they have similar methods in terms of basic functionality.
Method Availability:
- Vector: Has a few legacy methods like
addElement
,removeElement
, andcapacity
thatArrayList
does not have. - ArrayList: Uses the standard collection methods provided by the
List
interface.
Criteria | ArrayList | Vector |
---|---|---|
Synchronization | Not synchronized | Synchronized |
Performance | Faster because it is not synchronized | Generally slower due to synchronization |
Capacity Increase | By default, it increases the array size by 50% of its current size when full | Doubles the array size when full |
Methods | Only has methods defined by List interface | Has some legacy methods in addition to List interface methods |
Enumeration | Does not directly support Enumeration | Supports both Enumeration and Iterator |
Fail-Fast | Iterator is fail-fast | Iterator is fail-fast, but Enumeration is not |
Legacy | No, introduced in Java 1.2 | Yes, part of the original version of Java |
Use-case | Preferred when synchronization is managed externally or not required | When thread-safety is a priority, though better alternatives like Collections.synchronizedList or CopyOnWriteArrayList might be more suitable for certain modern applications |
Example: Difference Between ArrayList and Vector in Java
import java.util.ArrayList;
import java.util.Vector;
public class ArrayListVsVectorDemo {
public static void main(String[] args) {
// Create an ArrayList and add elements
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("apple");
arrayList.add("banana");
arrayList.add("cherry");
System.out.println("ArrayList:");
arrayList.forEach(System.out::println);
// Create a Vector and add elements
Vector<String> vector = new Vector<>();
vector.add("apple");
vector.add("banana");
vector.add("cherry");
System.out.println("\nVector:");
vector.forEach(System.out::println);
}
}
Output:
ArrayList: apple banana cherry Vector: apple banana cherry
Explanation:
1. ArrayList: An ArrayList is a resizable array implementation of the List interface. It's not synchronized, meaning it's not thread-safe. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies it structurally, it must be synchronized externally. This makes ArrayList more suitable for non-threaded applications or single-threaded portions of applications.
2. Vector: A Vector is similar to ArrayList, but it is synchronized, making it thread-safe. Every individual operation on a Vector is synchronized, ensuring that only one thread can access a method at a given time. Because of this, Vector can have performance overhead when used in a single-threaded environment, but it's safer to use in multi-threaded applications.
3. In the provided example, both the ArrayList and Vector have the same elements added in the same order, and when printed, they display the elements in the order they were added.
4. For most use cases in modern Java applications, ArrayList is preferred due to its non-synchronized nature and better performance in single-threaded scenarios. If thread-safety is required, consider using Collections.synchronizedList to wrap an ArrayList or use other concurrent collections like CopyOnWriteArrayList.