C

Core Java tutorial for beginners

Clean • Professional

Vector in Java – Features, Methods, Synchronization & Examples

2 minute

Vector in Java

Vector is a legacy class in Java that provides a dynamic array similar to ArrayList, but with built-in synchronization.

It is part of the java.util package and was introduced in JDK 1.0.

What is Vector?

A Vector is a dynamic, growable array in Java that stores elements and automatically increases its size.

It is similar to ArrayList but thread-safe because all methods are synchronized.

Where Vector Fits in Collection Hierarchy

Iterable
   └── Collection
          └── List
                └── Vector

Vector implements: List, RandomAccess, Cloneable, Serializable


Key Features of Vector (Short Points)

  • Grows dynamically when more elements are added
  • Thread-safe (all operations synchronized)
  • Maintains insertion order
  • Allows duplicates and null values
  • Legacy class (introduced in Java 1.0)

Why Vector is Called Legacy?

Vector is slower due to synchronization and was replaced by modern classes like ArrayList, but it still exists for backward compatibility.


Creating a Vector

Vector<String> vector = new Vector<>();
vector.add("Java");
vector.add("Python");
vector.add("C++");
System.out.println(vector);

Common Vector Methods

learn code with durgesh images

1. Add Methods

add(E e)

Adds an element to the end of the vector.

vector.add("PHP");

add(int index, E element)

Inserts an element at the specified position.

vector.add(1, "HTML");

addElement(E obj)

Legacy method; works same as add().

vector.addElement("Ruby");

2. Get Methods

get(int index)

Returns the element at the given index.

vector.get(1);

firstElement() / lastElement()

Returns the first or last element of the vector.

vector.firstElement();
vector.lastElement();

3. Remove Methods

remove(int index)

Removes the element at a specific index.

vector.remove(0);

removeElement(Object obj)

Legacy method to remove an element.

vector.removeElement("Java");

clear()

Removes all elements.

vector.clear();

4. Search Methods

contains(Object o)

Checks if the vector contains the given element.

vector.contains("Java");

indexOf(Object o)

Returns the first index of the element.

vector.indexOf("Python");

5. Size & Capacity Methods

size()

Returns the number of elements.

vector.size();

capacity()

Returns the internal capacity of the vector.

vector.capacity();

Understanding Vector Capacity

Vector starts with a default capacity of 10 and doubles its size automatically when full.

You can also set custom initial capacity or increment size manually.

Vector<Integer> vector = new Vector<>(5, 2);

Time Complexity of Vector

OperationTime ComplexityNotes
Access by indexO(1)Direct index lookup
Insert at endO(1) amortizedMay resize vector
Insert at indexO(n)Elements shift
Remove at indexO(n)Shifts required
SearchO(n)Linear search

Vector vs ArrayList

FeatureVectorArrayList
Thread-safeYesNo
SpeedSlowFast
LegacyYesNo
GrowthDoubles (×2)1.5×
Use CaseMultithreadingNormal use

Advantages of Vector

  • Thread-safe (synchronized)
  • Auto-expanding storage
  • Backward compatible with old code
  • Works well in multi-threaded apps

Disadvantages of Vector

  • Slower than ArrayList
  • Higher memory usage due to big growth
  • Considered outdated (legacy)
  • Synchronization cannot be disabled

Real-World Use Cases

Vector is still used in:

  • Multi-threaded applications
  • Legacy enterprise systems
  • Chat servers
  • Real-time multi-threaded programs

Article 0 of 0