Vector in Java


We will cover following points of vector in Java:

  1. Basics of Vector in Java
  2. How to iterate through vector in Java
  3. Constructor and methods of Vector in Java

Basics of Vector in Java

  1. Vector is a legacy class which introduced in JDK1.0 which implements List, RandomAccess, Cloneable and Serializable interface.
  2. It can store different types of element and maintain insertion order.
  3. Vector uses indexing representation to store the elements, so we can vector elements randomly.
  4. Vector allows duplicate elements.
  5. We can have null as an element in Vector.
  6. Most of the methods of Vector are synchronized.
  7. Vector objects are eligible for serialization and Cloning.
  8. Vector elements can be iterated through Iterator, Listiterator, and Enumeration.
  9. Stack is the direct Known subclass of Vector.

How to iterate vector in Java

  1. Using iterator
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class VectorExample {
     public static void main(String[] args) {
            List<String> list = new Vector<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
     Iterator<String> it = list.iterator();
       while (it.hasNext()) {
         System.out.println(it.next());
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using Enumeration
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class VectorExample {
     public static void main(String[] args) {
            Vector<String> list = new Vector<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
     Enumeration<String> enm = list.elements();
       while (enm.hasMoreElements()) {
         System.out.println(it.next());
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using ListIterator
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class VectorExample {
     public static void main(String[] args) {
            Vector<String> list = new Vector<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
      ListIterator<String> listIter = list.listIterator();
       while (listIter .hasNext()) {
        System.out.println(listIter.next());
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using For-each loop
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class VectorExample {
     public static void main(String[] args) {
            Vector<String> list = new Vector<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
       for (String str : list) {
         System.out.println(list.get(i));
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using For loop
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class VectorExample {
     public static void main(String[] args) {
            List<String> list = new Vector<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
           for(int i = 0; i < list.size();i++) { 
            System.out.println(list.get(i)); 
           }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash

Constructor and methods of vector in Java

Vector has mainly four constructor:

  • Vector() – We are passing 10 as the initial capacity.
  • Vector(Collection  c) –  We can convert Vector to other types of collections using this constructor. Internal implementation of this constructor –
  • Vector(int initialCapacity) – We can create a Vector object with some different capacity instead of default capacity 10. Internal implementation of this constructor-
  • Vector(int initialCapacity, int capacityIncrement) – We can create an empty vector with some different capacity also we can specify how new capacity will define. Internal implementation of this constructor –

Learn more about collection in java


%d bloggers like this: