Java – Vector Collection

Vector:

  • Vector implements List.
  • Vector allow duplicates and follow insertion order.
  • Vector is synchronized by default.
import java.util.*;
class Code
{
            public static void main(String[] args)
            {
                        Vector<Integer> v = new Vector<Integer>();
                        for (int i=1 ; i<=5 ; i++){
                                    v.add(i*5);
                        }
                        System.out.println(“Vector : ” + v);
            }
}

Enumeration:

  • Vector is legacy(old) class since first version of JDK.
  • Enumeration interface used to process vector element by element.
  • elements() method of Vector class returns Enumeration-interface.

Methods of Enumeration:

  1. hasMoreElements(): is used to check the element is present or not in Enumeration
  2. nextElement(): returns the next element in the enumeration.
import java.util.*;
class Code
{
      public static void main(String[] args)
      {
                  Vector<Integer> v = new Vector<Integer>();
                  for (int i=1 ; i<=5 ; i++){
                              v.add(i*5);
                  }
                  System.out.println(“Vector : “);
                  Enumeration<Integer> en = v.elements();
                  while(en.hasMoreElements())
                  {
                              Integer x = en.nextElement();
                              System.out.println(x);
                  }
      }
}
Scroll to Top