Java Vector equals() Method



Description

The Java Vector equals(Object o) method is used to compare the specified Object with this Vector for equality. It returns true if and only if the specified Object is also a List, both Lists have the same size, and all corresponding pairs of elements in the two Lists are equal.Two Lists are defined to be equal if they contain the same elements in the same order.

Declaration

Following is the declaration for java.util.Vector.equals() method

public boolean equals(Object o)

Parameters

o − This is the Object to be compared for equality with this Vector.

Return Value

The method call returns true if the specified Object is equal to this Vector.

Exception

NA

Checking two Vectors of Integer for equality Example

The following example shows the usage of Java Vector equals() method to check if two vectors are same or not. We're creating three vector objects of Integer. Then elements are added to each object. Two vector contains same elements and one is having different elements. Then using equals() method we're comparing these vectors and printing the status.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create two empty Vectors an initial capacity of 4      
      Vector<Integer> firstvec = new Vector<>(4);
      Vector<Integer> secondvec = new Vector<>(4);
	  Vector<Integer> thirdvec = new Vector<>(4);

      // adding in firstvec
      firstvec.add(4);
      firstvec.add(3);
      firstvec.add(2);

      // adding in secondvec
      secondvec.add(4);
      secondvec.add(3);
      secondvec.add(2);

      // adding in secondvec
      thirdvec.add(4);
      thirdvec.add(3);
      thirdvec.add(1); 

      // let us print if vectors are same or not
      System.out.println("Testing equality of firstvec and secondvec: "+ firstvec.equals(secondvec));
      System.out.println("Testing equality of firstvec and thirdvec: "+ firstvec.equals(thirdvec));	  
   } 
}

Output

Let us compile and run the above program, this will produce the following result.

Testing equality of firstvec and secondvec: true
Testing equality of firstvec and thirdvec: false

Checking two Vectors of String for equality Example

The following example shows the usage of Java Vector equals() method to check if two vectors are same or not. We're creating three vector objects of String. Then elements are added to each object. Two vector contains same elements and one is having different elements. Then using equals() method we're comparing these vectors and printing the status.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create two empty Vectors an initial capacity of 4      
      Vector<String> firstvec = new Vector<>(4);
      Vector<String> secondvec = new Vector<>(4);
	  Vector<String> thirdvec = new Vector<>(4);

      // adding in firstvec
      firstvec.add("D");
      firstvec.add("C");
      firstvec.add("B");

      // adding in secondvec
      secondvec.add("D");
      secondvec.add("C");
      secondvec.add("B");

      // adding in secondvec
      thirdvec.add("D");
      thirdvec.add("C");
      thirdvec.add("A"); 

      // let us print if vectors are same or not
      System.out.println("Testing equality of firstvec and secondvec: "+ firstvec.equals(secondvec));
      System.out.println("Testing equality of firstvec and thirdvec: "+ firstvec.equals(thirdvec));	  
   } 
}

Output

Let us compile and run the above program, this will produce the following result.

Testing equality of firstvec and secondvec: true
Testing equality of firstvec and thirdvec: false

Checking two Vectors of Object for equality Example

The following example shows the usage of Java Vector equals() method to check if two vectors are same or not. We're creating three vector objects of Student objects. Then elements are added to each object. Two vector contains same elements and one is having different elements. Then using equals() method we're comparing these vectors and printing the status.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create two empty Vectors an initial capacity of 4      
      Vector<Student> firstvec = new Vector<>(4);
      Vector<Student> secondvec = new Vector<>(4);
	  Vector<Student> thirdvec = new Vector<>(4);

      // use add() method to add elements in the vector firstvec
      firstvec.add(new Student(1, "Julie"));
      firstvec.add(new Student(2, "Robert"));
      firstvec.add(new Student(3, "Adam"));      

      // use add() method to add elements in the vector secondvec
      secondvec.add(new Student(1, "Julie"));
      secondvec.add(new Student(2, "Robert"));
      secondvec.add(new Student(3, "Adam"));      

      // use add() method to add elements in the vector thirdvec
      thirdvec.add(new Student(1, "Julie"));
      thirdvec.add(new Student(2, "Robert"));
      thirdvec.add(new Student(4, "Jane"));

      // let us print if vectors are same or not
      System.out.println("Testing equality of firstvec and secondvec: "+ firstvec.equals(secondvec));
      System.out.println("Testing equality of firstvec and thirdvec: "+ firstvec.equals(thirdvec));	  
   } 
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Testing equality of firstvec and secondvec: true
Testing equality of firstvec and thirdvec: false
java_util_vector.htm
Advertisements