Vectors in Java Collections

  • Vector is like the dynamic array which can grow or shrink its size. 
  • Vector is similar to ArrayList but threadsafe
  • It is recommended to use the Vector class in the thread-safe implementation only else use ArrayList
  • Iterator returned by Vector is fail-fast. Means any structural modification made to Vector like adding or removing elements during Iteration will throw java.util.ConcurrentModificationException.

It is similar to the ArrayList, but with two differences-

  • Vector is synchronized.
  • Java Vector contains many legacy methods that are not the part of a collections framework.

Example :

package java8;

import java.util.Vector;

public class VectorDemo {
	public static void main(String[] args) {
		 Vector<String> vec = new Vector<String>();  
         //Adding elements using add() method of List  
         //Adding elements using addElement() method of Vector  
         System.out.println("Elements are: "+vec);  

Since it is similar to arraylist, the complexity of Vector is same

Complexity of Vector

VectorTime ComplexitySpace Complexity
Adding ElementO(1) – resize O(log(n))
Removing ElementO(n)
Iterating ElementsO(n)
Retreving Element at PositionO(1)

Reference :

Leave a Comment