Java Vector equals(Object o) Example

The equals(Object o) method of java.util.Vector class will check specified Object o with the Vector for equality.

Signature

public boolean equals(Object o) 

equals(Object o) compares the specified Object with the Vector for equality. 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.

Example

The following example shows the usage of java.util.Vector.equals(o) method.

import java.util.Vector;

public class EqualsMethodVectorExample 
{
    public static void main(String args[])
    {
	    // Create new Vector vector1 
	    Vector vector1 = new Vector();
	    
	    //Adding elements to the Vector2
	    vector1.add("Element1");
	    vector1.add("Element2");
	    vector1.add("Element3");
	    vector1.add("Element4");
	    
	    // Create new Vector vector2 
        Vector vector2 = new Vector();
        
        //Adding elements to the Vector2
        vector2.add("Element1");
        vector2.add("Element2");
        vector2.add("Element3");
        vector2.add("Element4");
        
        //Checking the vector1 and vector2 equality
        System.out.println("Is vector1 equal to vector2 : "+vector1.equals(vector2));
        
        vector2.add("Element5");
        
        //Lets check again
        System.out.println("Is vector1 equal to vector2 : "+vector1.equals(vector2));
    }
}

Output

Is vector1 equal to vector2 : true
Is vector1 equal to vector2 : false

Leave a Reply

Your email address will not be published. Required fields are marked *