Java Vector equals() Method
\n\n\n\nThe equals() method is an important method provided by the Vector class in Java, used to compare whether two Vector objects are equal. This method is inherited from the AbstractList class and implements the equals() method definition from the List interface.
Syntax Format
\npublic boolean equals(Object o)\n\nMethod Parameters
\n- \n
o: The object to compare with the currentVector\n- \n
- Type:
Object\n - Can be any Java object, but content comparison is only performed if it is of type
List\n
\n- Type:
Return Value
\n- \n
- Returns a
booleanvalue:\n- \n
true: If the specified object is also aList, both lists have the same size, and all corresponding element pairs are equal \n false: All other cases \n
\n
\n\n
Method Features
\n\n1. Content Comparison vs. Reference Comparison
\nThe equals() method compares the content of the Vector rather than the reference address. Even if two Vectors are different object instances, equals() will return true as long as they contain the same elements in the same order.
Example
\nVector v1 =new Vector(Arrays.asList("A", "B", "C"));\n\n Vector v2 =new Vector(Arrays.asList("A", "B", "C"));\n\nSystem.out.println(v1.equals(v2));// Output true\n\n2. Compatibility with the List Interface
\nSince Vector implements the List interface, the equals() method can be compared with any List implementation, including ArrayList, LinkedList, etc.
Example
\nVector vector =new Vector(Arrays.asList(1, 2, 3));\n\n ArrayList arrayList =new ArrayList(Arrays.asList(1, 2, 3));\n\nSystem.out.println(vector.equals(arrayList));// Output true\n\n3. Order Sensitivity of Elements
\nThe equals() method is sensitive to the order of elements. Even if the same elements are present but in a different order, it will return false.
Example
\nVector v1 =new Vector(Arrays.asList("A", "B", "C"));\n\n Vector v2 =new Vector(Arrays.asList("C", "B", "A"));\n\nSystem.out.println(v1.equals(v2));// Output false\n\n\n\n
Implementation Principle
\nThe equals() method of Vector is actually inherited from AbstractList, and its core implementation logic is as follows:
- \n
- First, check if the compared object is the same instance (reference equality) \n
- Check if the object is of type
List\n - Compare whether the sizes of the two lists are the same \n
- Compare elements at each corresponding position for equality (using the element's
equals()method) \n
\n\n
Precautions
\n\n1. Element Objects' equals() Method
\nThe equals() method of Vector relies on the equals() method implementation of its elements. If storing custom objects, ensure that the equals() method has been correctly overridden.
2. Handling null Values
\nVector can contain null elements, and the equals() method can correctly handle comparisons involving null values.
Example
\nVector v1 =new Vector(Arrays.asList(null, "B"));\n\n Vector v2 =new Vector(Arrays.asList(null, "B"));\n\nSystem.out.println(v1.equals(v2));// Output true\n\n3. Performance Considerations
\nFor large Vectors, the equals() method requires linear time comparison (O(n) time complexity), which should be noted in performance-sensitive scenarios.
\n\n
Sample Code
\n\nExample
\nimport java.util.Vector;\n\nimport java.util.Arrays;\n\npublic class VectorEqualsExample {\n\npublic static void main(String[] args){\n\n// Create three Vectors\n\n Vector vector1 =new Vector(Arrays.asList("Java", "Python", "C++"));\n\n Vector vector2 =new Vector(Arrays.asList("Java", "Python", "C++"));\n\n Vector vector3 =new Vector(Arrays.asList("Python", "Java", "C++"));\n\n// Compare vector1 and vector2\n\nSystem.out.println("vector1 equals vector2: "+ vector1.equals(vector2));// true\n\n// Compare vector1 and vector3\n\nSystem.out.println("vector1 equals vector3: "+ vector1.equals(vector3));// false\n\n// and ArrayList Compare\n\n java.util.ArrayList arrayList =\n\nnew java.util.ArrayList(Arrays.asList("Java", "Python", "C++"));\n\nSystem.out.println("vector1 equals arrayList: "+ vector1.equals(arrayList));// true\n\n}\n\n}\n\n\n\n
Comparison with Other Methods
\n\nequals() vs ==
\n- \n
equals(): Compares content \n ==: Compares reference addresses \n
Example
\nVector v1 =new Vector();\n\n Vector v2 =new Vector();\n\n Vector v3 = v1;\n\nSystem.out.println(v1.equals(v2));// true\n\nSystem.out.println(v1 == v2);// false\n\nSystem.out.println(v1 == v3);// true\n\nequals() vs containsAll()
\n- \n
equals(): Requires exact match in element order and quantity \n containsAll(): Only checks if all elements are contained, ignoring order and quantity \n
\n\n
Summary
\nThe equals() method of Vector provides content-level comparison functionality and is the standard method for determining whether two Vectors are equal in content. Understanding its working principle and characteristics is crucial for correctly using the Vector collection. In practical programming, choose the appropriate comparison method based on specific requirements, and pay attention to the implementation of the equals() method for custom objects.
YouTip