Vector Class in Java

In this article, we are going to learn about Vector class of Java. Here, we will explain about it with the help of example.
Submitted by Jyoti Singh, on February 09, 2018

Vector is a collection which holds heterogeneous Objects that are it can hold objects of different class type and then each object is type casted to its original class. Vector is synchronized that is multiple threads can work on a vector simultaneously which makes vector slow.

    Vector V = new Vector ();
    V.add(new Student('100','ramesh'));
    V.add(new Company('101','Airtel'));

Here, Vector V holds two objects of different classes Student and Object.

We can also add integer, float or string type values in vector as object. These values are not needed to typecast.

Advantages of Vector

  1. Vectors can hold Objects of different classes.
  2. Vectors are synchronous.

Disadvantage of Vector

Vector is Slow

Let's understand vector more clearly using the below example:

import java.util.Vector;

// class representing the Student 
class Student {
  // data members of class	
  private int rollno;
  private String name;
  private String schoolCode;

  // Constructor
  public Student(int rollno, String name, String schoolcode) {
    this.rollno = rollno;
    this.name = name;
    this.schoolCode = schoolcode;
  }
  
  // putStudent() to print the values of the student Object
  public void putStudent() {
    System.out.println("RollNo :" + this.rollno + "\nName :" + this.rollno + "\nSchoolCode :" + this.schoolCode);
  }
}

// class representing the employee
class CompanyEmployee {
  // data memebers of the class
  public int id;
  public String name;
  public long salary;
  
  // constructor
  public CompanyEmployee(int id, String name, long salary) {
    this.id = id;
    this.name = name;
    this.salary = salary;
  }
  
  // putEmployee() to print the values of the employee object
  public void putEmployee() {
    System.out.println("Id :" + this.id + "\nName :" + this.name + "\nSalary :" + this.salary);
  }
}

// main class 
public class ExVector {
  public static void main(String[] args) {
    Vector V = new Vector(); //Vector class to hold the objects
    // adding CompanyEmployee Object to Vector 
    V.add(new CompanyEmployee(100, "Harendra Chekkur", 34000)); 
    // adding Student Object to Vector
    V.add(new Student(10, "Madhav Singh", "CB2025")); 
    // adding Integer as Object to Vector
    V.add(new Integer(70)); 
    // adding String as an Object to Vector
    V.add(new String("Testing Vectors")); 
    // adding employee as Object to Vector
    V.add(new Float(57.4)); 

    // iterating the vector to print the Objects 
    for (Object O: V) {
      /* as Vector holds hetrogeneous data objects,
      thus we have to cast the object to it's type
      in order to do this we are using getName() function 
      which gets the name of the class of the given object 
      and compares it with the given class , 
      if it's matches than typecast the object to that class */
      if (O.getClass().getName().equals("logicProgramming.CompanyEmployee")) {
        System.out.println();
        ((CompanyEmployee) O).putEmployee();
      } else if (O.getClass().getName().equals("logicProgramming.Student")) {
        System.out.println();
        ((Student) O).putStudent();
      }
      // if no match is found that is we will simply print th Object 
      else {
        System.out.println();
        System.out.println(O);
      }
    }
  }
}

Output

Id :100
Name :Harendra Chekkur
Salary :34000

RollNo :10
Name :10
SchoolCode :CB2025

70

Testing Vectors

57.4


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.