Java Vector Class

The Vector class implements a dynamic array of objects. Vector is synchronized collection. The Vector class has extended AbstractList class and implemented List and RandomAccess interface.

Default Capacity of Vector Class

Default capacity of Vector class is 10. But you can specify the capacity of vector while creating object.

Difference between ArrayList and Vector

Both ArrayList and Vector class has implemented List interface. But there are following difference between them.

ArrayList –

  • ArrayList is not a legacy class, it is introduced in JDK 1.2.
  •  ArrayList is not synchronized.
  • ArrayList increments 50% of current array size if number of element exceeds from its capacity.
  • ArrayList is fast because it is non-synchronized.
  • ArrayList uses Iterator interface to traverse the elements.

Vector –

  • Vector is a legacy class. Legacy classes and interfaces are the classes and interfaces that formed the collections framework in the earlier versions of Java and how now been restructured or re-engineered. They are fully compatible with the framework.
  • Vector is synchronized.
  • Vector increments 100% means doubles the array size if total number of element exceeds than its capacity.
  • Vector is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.
  • Vector has both Enumeration and Iterator interface to traverse the elements.

Constructors of Vector class

  1. Vector()
      Vector v = new Vector();

    This will create vector object with default capacity 10 and while adding 11th element vector capacity is resized to 20.

  2. Vector(int initialCapacity)

      Vector v = new Vector(3);

    This will create vector object with capacity 3 and while adding 4th element vector capacity is resized to 6.

  3. Vector(int initialcapacity, capacityIncrement)

    Vector v = new Vector(4,6); 

    Here there are two arguments. The initial capacity is 4 and capacityIncrement is 6. In that case while insertion of 5th element the size would be 10 (4+6) and on 11th insertion it would be 16(10+6).

Commonly used methods of Vector Class:

  1. void addElement(Object element): It adds the element at the end of the Vector.
  2. int capacity(): It returns the current capacity of the vector.
  3. int size(): It returns the current size of the vector.
  4. void setSize(int size): It changes the existing size with the given size.
  5. boolean contains(Object element): This method checks whether the specified element is present in the Vector. If the element is been found it returns true otherwise returns false.
  6. boolean containsAll(Collection c): It returns true if all the elements of collection c are present in the Vector.
  7. Object elementAt(int index): It returns the element present at the specified location in Vector.
  8. Object firstElement(): It is used for getting the first element of the vector.
  9. Object lastElement(): It returns the last element of the array.
  10. Object get(int index): It returns the element at the specified index.
  11. boolean isEmpty(): It returns true if Vector doesn’t have any element.
  12. boolean removeElement(Object element): Removes the specified element from vector.
  13. boolean removeAll(Collection c): It Removes all those elements from vector which are present in the Collection c.
  14. void setElementAt(Object element, int index): It updates the element of specified index with the given element.

Complete Example of Vector class:

package com.startwithjava;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

public class VectorMain {
	public static void main(String[] args) {
	 /*Here default capacity is 4 */
	 Vector<String> v = new Vector<String>(4);
	/* Adding elements to a vector*/
	 v.addElement("Item 1");
	 v.addElement("Item 2");
	 v.addElement("Item 3");     
	 /* print size and capacityIncrement*/
	   System.out.println("Size is: "+v.size());
	   System.out.println("Capacity is: "+v.capacity());
	     v.addElement("Item 4");
	     v.addElement("Item 5");
	     v.addElement("Item 6");
	     v.addElement("Item 7");
	 /*size and capacityIncrement after two insertions*/
	   System.out.println("Size after addition: "+v.size());
	   System.out.println("Capacity after addition: "+v.capacity());
	 /*Display Vector elements using Enumeration*/
	  Enumeration en = v.elements();
	  System.out.println("\nTraversing elements using Enumeration:");

          /*Display Vector elements using Enumeration*/
	   Iterator itr = v.iterator();
	   System.out.println("\nTraversing elements using Iterator:");




Size is: 3
Capacity is: 4
Size after addition: 7
Capacity after addition: 8

Traversing elements using Enumeration:
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7

Traversing elements using Iterator:
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7


Leave a Reply