Understanding Java Vector: Usage, Features, and Comparison with ArrayList
Understanding Java Vector
This guide explains the Java Vector class, its key methods, and how it differs from ArrayList. Learn when to choose Vector for thread‑safety and performance considerations.
The Vector class implements the List interface and offers a resizable array similar to ArrayList. Unlike ArrayList, every mutating operation on a Vector is synchronized, making it inherently thread‑safe but potentially slower in single‑threaded contexts.
Java Vector vs. ArrayList
Both ArrayList and Vector provide dynamic arrays, yet they differ in concurrency handling:
- Vector synchronizes each method call, acquiring a lock on the instance before execution.
- ArrayList is unsynchronized by default; thread‑safety can be added with
Collections.synchronizedList()if needed.
Because of the per‑operation lock, Vector can incur overhead in multi‑threaded environments, whereas ArrayList usually offers better performance when thread safety is not required.
Tip: Prefer ArrayList for most applications. Use Vector only when legacy code demands it or when you need built‑in synchronization without additional wrappers.
Creating a Vector
You can instantiate a Vector with a generic type:
Vector<Type> vector = new Vector<>();
Examples:
// Integer vector
Vector<Integer> numbers = new Vector<>();
// String vector
Vector<String> words = new Vector<>();
Key Methods of Vector
The Vector class offers a full set of list operations, mirroring ArrayList. Some commonly used methods include:
add(E element)– append an element.add(int index, E element)– insert at a specific position.addAll(Collection<? extends E> c)– bulk add.get(int index)– retrieve by index.remove(int index)– delete by index.clear()– remove all elements.size()– current number of elements.contains(Object o)– check for existence.toArray()– convert to array.toString()– string representation.set(int index, E element)– replace at index.
Adding Elements to a Vector
add(element)– appends the element.add(index, element)– inserts at a specific index.addAll(vector)– adds all elements from another vector.
Example:
import java.util.Vector;
class Main {
public static void main(String[] args) {
Vector<String> mammals = new Vector<>();
mammals.add("Dog");
mammals.add("Horse");
mammals.add(2, "Cat");
System.out.println("Vector: " + mammals);
Vector<String> animals = new Vector<>();
animals.add("Crocodile");
animals.addAll(mammals);
System.out.println("New Vector: " + animals);
}
}
Output
Vector: [Dog, Horse, Cat] New Vector: [Crocodile, Dog, Horse, Cat]
Accessing Vector Elements
get(index)– retrieve element by index.iterator()– obtain an iterator for sequential access.
Example:
import java.util.Iterator;
import java.util.Vector;
class Main {
public static void main(String[] args) {
Vector<String> animals = new Vector<>();
animals.add("Dog");
animals.add("Horse");
animals.add("Cat");
String element = animals.get(2);
System.out.println("Element at index 2: " + element);
Iterator<String> iter = animals.iterator();
System.out.print("Vector: ");
while(iter.hasNext()) {
System.out.print(iter.next() + ", ");
}
}
}
Output
Element at index 2: Cat Vector: Dog, Horse, Cat,
Removing Elements from a Vector
remove(index)– delete the element at the specified position.clear()– efficiently remove all elements.
Example:
import java.util.Vector;
class Main {
public static void main(String[] args) {
Vector<String> animals = new Vector<>();
animals.add("Dog");
animals.add("Horse");
animals.add("Cat");
System.out.println("Initial Vector: " + animals);
String removed = animals.remove(1);
System.out.println("Removed Element: " + removed);
System.out.println("New Vector: " + animals);
animals.clear();
System.out.println("Vector after clear(): " + animals);
}
}
Output
Initial Vector: [Dog, Horse, Cat] Removed Element: Horse New Vector: [Dog, Cat] Vector after clear(): []
Additional Vector Methods
| Method | Description |
|---|---|
set(int index, E element) |
Replace the element at the specified position. |
size() |
Return the number of elements. |
toArray() |
Convert the contents to an array. |
toString() |
String representation of the vector. |
contains(Object o) |
Check if the vector contains the specified element. |
Java
- Master Java Operators: Types, Syntax, & Practical Examples
- Java Comments: Types, Usage, and Best Practices
- Mastering Java if…else: Control Flow Explained
- Mastering Java's super Keyword: Advanced Usage & Practical Examples
- Mastering Java Interfaces: Concepts, Implementation, and Best Practices
- Mastering Java Try‑with‑Resources: Automatic Resource Management Explained
- Java Annotations Explained: Types, Placement, and Practical Examples
- Mastering Java ArrayList: Operations, Methods, and Best Practices
- Java LinkedList: Comprehensive Guide with Examples
- Java 8: A Comprehensive Quick Reference Guide