Vector equals() Method in Java
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)); } } |
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)); } } |
The Vector is: [10, 15, 20, 25, 30] The Vector is: [10, 15, 20, 25, 30, 40] Are both of them equal? false
Recommended Posts:
- BigInteger equals() Method in Java
- AbstractSequentialList equals() method in Java with Example
- IdentityHashMap equals() Method in Java
- MathContext equals() Method in Java
- ConcurrentSkipListSet equals() method in Java
- YearMonth equals() method in Java
- DecimalStyle equals() method in Java with Example
- TreeSet equals() method in Java with Example
- GregorianCalendar equals() Method in Java
- Calendar.equals() method in Java
- Difference between == and .equals() method in Java
- LinkedHashSet equals() method in Java with Example
- DecimalFormat equals() method in Java
- ConcurrentLinkedDeque equals() method in Java with Example
- IntBuffer equals() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



