Java Utility Library

Java Vector - equals() Method



The java.util.Vector.equals() method is used to compare the specified object with the vector for equality. It returns true 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. In other words, two Lists are defined to be equal if they contain the same elements in the same order.

Syntax

public boolean equals(Object obj)

Parameters

obj Specify the object to be compared for equality with the vector.

Return Value

Returns true if the specified Object is equal to the vector, false otherwise.

Exception

NA.

Example:

In the example below, the java.util.Vector.equals() method is used to compare a object with the given vector for equality.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating three vector
    Vector<Integer> vec1 = new Vector<Integer>();
    Vector<Integer> vec2 = new Vector<Integer>();
    Vector<Integer> vec3 = new Vector<Integer>();

    //populating vec1
    vec1.add(10);
    vec1.add(20);

    //populating vec2
    vec2.add(10);
    vec2.add(20);
    
    //populating vec3
    vec3.add(20);
    vec3.add(10);

    //check if vec1 and vec2 are equal
    System.out.println("vec1 and vec2 are equal? - " + vec1.equals(vec2));

    //check if vec1 and vec2 are equal
    System.out.println("vec1 and vec3 are equal? - " + vec1.equals(vec3));
  }
}

The output of the above code will be:

vec1 and vec2 are equal? - true
vec1 and vec3 are equal? - false

❮ Java.util - Vector