The Wayback Machine - https://web.archive.org/web/20240618020224/https://www.geeksforgeeks.org/vector-equals-method-in-java/
Open In App

Vector equals() Method in Java

Last Updated : 17 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.vector.equals(Object obj) method of Vector class in Java is used verify the equality of an Object with a vector and compare them. The list returns true only if both Vector contains same elements with same order.

Syntax:

first_vector.equals(second_vector)

Parameters: This method accepts a mandatory parameter second_vector which refers to the second Vector to be compared to the first Vector.

Return value: The method returns true if the equality holds and both the objects and vector are equal else it returns false.

Below programs are used to illustrate the working of the java.util.Vector.elements() method:

Program 1:




// Java code to illustrate the equals() method
import java.util.*;
  
public class Vector_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty Vector
        Vector<String> vec_tor1 = new Vector<String>(5);
  
        // Inserting elements into the table
        vec_tor1.add("Geeks");
        vec_tor1.add("4");
        vec_tor1.add("Geeks");
        vec_tor1.add("Welcomes");
        vec_tor1.add("You");
  
        // Displaying the Vector
        System.out.println("The Vector is: "
                           + vec_tor1);
  
        // Creating an empty Vector
        Vector<String> vec_tor2 = new Vector<String>(5);
  
        // Inserting elements into the table
        vec_tor2.add("Geeks");
        vec_tor2.add("4");
        vec_tor2.add("Geeks");
        vec_tor2.add("Welcomes");
        vec_tor2.add("You");
  
        // Displaying the Vector
        System.out.println("The Vector is: "
                           + vec_tor2);
  
        System.out.println("Are both of them equal? "
                           + vec_tor1.equals(vec_tor2));
    }
}


Output:

The Vector is: [Geeks, 4, Geeks, Welcomes, You]
The Vector is: [Geeks, 4, Geeks, Welcomes, You]
Are both of them equal? true

Program 2 :




// Java code to illustrate the equals() method
import java.util.*;
  
public class Vector_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty Vector
        Vector<Integer> vec_tor1 = new Vector<Integer>(5);
  
        // Inserting elements into the table
        vec_tor1.add(10);
        vec_tor1.add(15);
        vec_tor1.add(20);
        vec_tor1.add(25);
        vec_tor1.add(30);
  
        // Displaying the Vector
        System.out.println("The Vector is: " + vec_tor1);
  
        // Creating an empty Vector
        Vector<Integer> vec_tor2 = new Vector<Integer>(6);
  
        // Inserting elements into the table
        vec_tor2.add(10);
        vec_tor2.add(15);
        vec_tor2.add(20);
        vec_tor2.add(25);
        vec_tor2.add(30);
        vec_tor2.add(40);
  
        // Displaying the Vector
        System.out.println("The Vector is: " + vec_tor2);
  
        System.out.println("Are both of them equal? "
                           + vec_tor1.equals(vec_tor2));
    }
}


Output:

The Vector is: [10, 15, 20, 25, 30]
The Vector is: [10, 15, 20, 25, 30, 40]
Are both of them equal? false


Previous Article
Next Article

Similar Reads

Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
Method Class | equals() Method in Java
The java.lang.reflect.Method.equals(Object obj) method of Method class compares this Method Object against the specified object as parameter to equal(object obj) method. This method returns true if the Method object is the same as the passed object. Two Methods are the same if they were declared by the same class and have the same name and formal p
3 min read
Java.util.Arrays.equals() in Java with Examples
Today we are going to discuss the simplest way to check whether two arrays are equal or not. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two arra
4 min read
Calendar.equals() method in Java
java.util.Calendar.equals() is a method in Calendar class of java.util package. The method compares this Calendar to the specified Object.The method returns true if this object is equal to object. If this is not the case, i.e, if there is any difference in the parameters between the two Calendars, then false is returned. Syntax : public boolean equ
2 min read
Java Long equals() method with Examples
The java.lang.Long.equals() is a built-in function in java that compares this object to the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object. It returns false if both the objects are not same. In all other cases, compareTo method should be preferred. S
3 min read
Character.equals() method in Java with examples
The java.lang.Character.equals() is a function in Java which compares this object against the specified object. If the argument is not null then the result is true and is a Character object that represents the same char value as this object. Syntax: public boolean equals(Object obj) Parameters: The function accepts a single parameter obj which spec
2 min read
Double.equals() Method in Java with Examples
The java.lang.Double.equals() is a built-in function in java that compares this object to the specified object. The result is true if and only if the argument is not null and is a Double object that contains the same double value as this object. It returns false if both the objects are not same. In all other cases, compareTo method should be prefer
2 min read
MathContext equals() Method in Java
The java.math.MathContext.equals() is an in-built function in Java which checks for equality between this MathContext object with the object passed as parameter to the function. The function returns true if the context settings of both the aforementioned objects are same. Syntax : public boolean equals(Object obj) Parameters : The function accepts
2 min read
BigDecimal equals() Method in Java
The java.math.BigDecimal.equals() method checks for equality of a BigDecimal value with the object passed. This method considers two BigDecimal objects equal if only if they are equal in value and scale. Syntax: public boolean equals(Object obj) Parameters: This function accepts an Object obj as a mandatory parameter for comparison with this BigDec
2 min read
EnumMap equals() Method in Java with Examples
The Java.util.EnumMap.equals(obj) in Java is used to compare the passed object with this EnumMap for the equality. It must be kept in mind that the object passed must be a map of the same type as the EnumMap.Syntax: boolean equals(Object obj) Parameter: The method takes one parameter obj of Object type and refers to the map to be compared with this
2 min read