- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
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