INTRODUCTION OF VECTOR

Introduction Of Vector


Vector:

  • Another important type of List interface is Vector.

  • Vector class is fully Synchronized class.

  • Vector class is same as ArrayList. Except “Thread Safety”. means every method (or) every member inside vector is “Synchronized “ one i.e, at a time only One Thread can execute (or) Access those methods at any point of time.

  • Vector class will give less performance due to synchronization. But it will give more Accuracy.

  • Incaseof Arraylist we can achieve more performance and less accuracy. Why because ArrayList will allow multiple threads at a time due to unsynchronization .

  • In case of vector we will get 100% Threadsafety but incase of ArrayList there is no ThreadSafety.

Important Methods in Vector:

contains():

  • contain() will check whether the given element is present or not in the current List. If it is present then return Boolean True else False.

  • Syntax:

      public boolean contains(Object elem);

    Example 1:

     
                
                  package com.sdj.pack1;
                  public class Vector
                  {
                    public static void main(String[] args)
                    {
                      Vector v=new Vector();
                      v.add("20");
                      v.add("33");
                      v.add("44");
                      v.add("66");
                      System.out.println(v);
                      System.out.println(v.contains("ram")); /* false */
                      System.out.println(v.contains("33"));  /* true */
                    }
                  } 
                      

    Output:

         [20, 33, 44, 66]
         false
         true

    clear():

  • clear() will remove all the elements from the List.

  • Syntax:

      public void clear();

    Example 2:

     
                
                  package com.sdj.pack1;
                  public class Vector
                  {
                    public static  void main(String[] args)
                    {
                      Vector v=new Vector();
                      v.add("20");
                      v.add("33");
                      v.add("44");
                      v.add("66");
                      System.out.println(v);
                      v.clear();
                      System.out.println(v);
                    }
                  } 
                      

    Output:

         Before call Clear: [20, 33, 44, 66]
         After call Clear: [] // its deleted all elements from list.