Java Vector equals() Method
equals()The method is in JavaVectoran important method provided by the class, used to compare twoVectorobjects for equality. This method inherits fromAbstractListclass, and implements theListinterface'sequals()method definition.
Syntax Format
public boolean equals(Object o)
Method Parameters
o: To be compared with the currentVectorThe object to be compared- Type:
Object - Can be any Java object, but only when it is of
Listtype will content comparison be performed
- Type:
Return Value
- Returns
booleantype:true: If the specified object is also aList, and both lists have the same size, and all corresponding element pairs are equalfalse: All other cases
Method Features
1. Content Comparison Rather Than Reference Comparison
equals()The method comparesVectorcontent rather than reference addresses. Even if twoVectorare different object instances, as long as they contain the same elements in the same order,equals()will returntrue。
Example
Vector<String> v2 = new Vector<>(Arrays.asList("A", "B", "C"));
System.out.println(v1.equals(v2)); // Outputs true
2. Compatible with the List Interface
SinceVectorimplementsListinterface,equals()method can be compared with anyListimplementation, includingArrayList、LinkedListetc.
Example
ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1, 2, 3));
System.out.println(vector.equals(arrayList)); // Outputs true
3. Sensitive to Element Order
equals()The method is sensitive to element order. Even if the same elements are included but in a different order, it will returnfalse。
Example
Vector<String> v2 = new Vector<>(Arrays.asList("C", "B", "A"));
System.out.println(v1.equals(v2)); // Outputs false
Implementation Principle
Vectorofequals()The method actually inherits fromAbstractList, and its core implementation logic is as follows:
- First, check whether the compared object is the same instance (reference equality)
- Check whether the object is of
Listtype - Compare whether the sizes of the two lists are the same
- Compare one by one whether the elements at corresponding positions are equal (using the element's
equals()method)
Notes
1. The equals() Method of Element Objects
Vectorofequals()The method relies on theequals()method implementation of its elements. If custom objects are stored, you need to ensure that theequals()method is correctly overridden.
2. Handling null Values
VectorCan containnullelements,equals()the method can correctly handlenullcomparison of values.
Example
Vector<String> v2 = new Vector<>(Arrays.asList(null, "B"));
System.out.println(v1.equals(v2)); // Outputs true
3. Performance Considerations
For largeVector,equals()the method requires linear-time comparison (O(n) time complexity), which needs attention in performance-sensitive scenarios.
Example Code
Example
import java.util.Arrays;
public class VectorEqualsExample {
public static void main(String[] args) {
// Create three Vectors
Vector<String> vector1 = new Vector<>(Arrays.asList("Java", "Python", "C++"));
Vector<String> vector2 = new Vector<>(Arrays.asList("Java", "Python", "C++"));
Vector<String> vector3 = new Vector<>(Arrays.asList("Python", "Java", "C++"));
// Compare vector1 and vector2
System.out.println("vector1 equals vector2: " + vector1.equals(vector2)); // true
// Compare vector1 and vector3
System.out.println("vector1 equals vector3: " + vector1.equals(vector3)); // false
// Compare with ArrayList
java.util.ArrayList<String> arrayList =
new java.util.ArrayList<>(Arrays.asList("Java", "Python", "C++"));
System.out.println("vector1 equals arrayList: " + vector1.equals(arrayList)); // true
}
}
Comparison with Other Methods
equals() vs ==
equals(): Compares content==: Compares reference addresses
Example
Vector<String> v2 = new Vector<>();
Vector<String> v3 = v1;
System.out.println(v1.equals(v2)); // true
System.out.println(v1 == v2); // false
System.out.println(v1 == v3); // true
equals() vs containsAll()
equals(): Requires exactly the same element order and countcontainsAll(): Only checks whether all elements are contained, regardless of order and count
Summary
Vectorofequals()The method provides content-level comparison functionality and is the standard method for determining whether twoVectorare equal in content. Understanding its working principles and characteristics is important for correctly usingVectorcollections. In actual programming, you should choose the appropriate comparison method based on specific needs, and pay attention to theequals()method implementation of custom objects.
Java Vector