Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

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:

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:


Adding Elements to a 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

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

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

  1. Master Java Operators: Types, Syntax, & Practical Examples
  2. Java Comments: Types, Usage, and Best Practices
  3. Mastering Java if…else: Control Flow Explained
  4. Mastering Java's super Keyword: Advanced Usage & Practical Examples
  5. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  6. Mastering Java Try‑with‑Resources: Automatic Resource Management Explained
  7. Java Annotations Explained: Types, Placement, and Practical Examples
  8. Mastering Java ArrayList: Operations, Methods, and Best Practices
  9. Java LinkedList: Comprehensive Guide with Examples
  10. Java 8: A Comprehensive Quick Reference Guide