Vector Overview

Vector is very similar to ArrayList except that Vector is synchronized collection unlike ArrayList which is non-synchronized

Features of Vector are as below

Vector maintains insertion order
Vector allows duplicate elements
Vector is synchronized collection

Since vector is synchronized, it takes performance hit and hence its not good to use in non-threaded environment

Vector can be instantiated as

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

It creates an empty Vector with the default initial capacity of 10

Example :

  1. import java.util.*;
  2.  
  3. public class VectorExample {
  4.  
  5.    public static void main(String args[]) {
  6.      
  7.       Vector<String> vector = new Vector<String>();
  8.  
  9.      
  10.       vector.addElement("c");
  11.       vector.addElement("c++");
  12.       vector.addElement("java");
  13.    
  14.       System.out.println("Size of Vector is: "+vector.size());
  15.       System.out.println("Capacity before increment is: "+vector.capacity());
  16.       vector.addElement("machine learning");
  17.       vector.addElement("big data");
  18.       vector.addElement("cloud");
  19.  
  20.    
  21.       System.out.println("Size after addition: "+vector.size());
  22.       System.out.println("Capacity after increment is: "+vector.capacity());
  23.  
  24.          Enumeration en = vector.elements();
  25.       System.out.println("\nElements are:");
  26.       while(en.hasMoreElements())
  27.          System.out.print(en.nextElement() + " ");
  28.    }
  29. }
import java.util.*;

public class VectorExample {

   public static void main(String args[]) {
     
      Vector<String> vector = new Vector<String>();

     
      vector.addElement("c");
      vector.addElement("c++");
      vector.addElement("java");
    
      System.out.println("Size of Vector is: "+vector.size());
      System.out.println("Capacity before increment is: "+vector.capacity());
      vector.addElement("machine learning");
      vector.addElement("big data");
      vector.addElement("cloud");

    
      System.out.println("Size after addition: "+vector.size());
      System.out.println("Capacity after increment is: "+vector.capacity());

         Enumeration en = vector.elements();
      System.out.println("\nElements are:");
      while(en.hasMoreElements())
         System.out.print(en.nextElement() + " ");
   }
}
Note : Capacity will be doubled after adding 11th element to the Vector as Default capacity is 10

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on