YouTip LogoYouTip

Java Vector Equals

Java Vector equals() Method

\n\n

Java Vector Java Vector

\n\n

The 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.

\n\n

Syntax Format

\n
public boolean equals(Object o)
\n\n

Method Parameters

\n
    \n
  • o: The object to compare with the current Vector\n
      \n
    • Type: Object
    • \n
    • Can be any Java object, but content comparison is only performed if it is of type List
    • \n
    \n
  • \n
\n\n

Return Value

\n
    \n
  • Returns a boolean value:\n
      \n
    • true: If the specified object is also a List, both lists have the same size, and all corresponding element pairs are equal
    • \n
    • false: All other cases
    • \n
    \n
  • \n
\n\n
\n\n

Method Features

\n\n

1. Content Comparison vs. Reference Comparison

\n

The 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.

\n\n

Example

\n
Vector 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\n

2. Compatibility with the List Interface

\n

Since Vector implements the List interface, the equals() method can be compared with any List implementation, including ArrayList, LinkedList, etc.

\n\n

Example

\n
Vector 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\n

3. Order Sensitivity of Elements

\n

The 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.

\n\n

Example

\n
Vector 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

\n

The equals() method of Vector is actually inherited from AbstractList, and its core implementation logic is as follows:

\n
    \n
  1. First, check if the compared object is the same instance (reference equality)
  2. \n
  3. Check if the object is of type List
  4. \n
  5. Compare whether the sizes of the two lists are the same
  6. \n
  7. Compare elements at each corresponding position for equality (using the element's equals() method)
  8. \n
\n\n
\n\n

Precautions

\n\n

1. Element Objects' equals() Method

\n

The 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.

\n\n

2. Handling null Values

\n

Vector can contain null elements, and the equals() method can correctly handle comparisons involving null values.

\n\n

Example

\n
Vector 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\n

3. Performance Considerations

\n

For large Vectors, the equals() method requires linear time comparison (O(n) time complexity), which should be noted in performance-sensitive scenarios.

\n\n
\n\n

Sample Code

\n\n

Example

\n
import 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\n

equals() vs ==

\n
    \n
  • equals(): Compares content
  • \n
  • ==: Compares reference addresses
  • \n
\n\n

Example

\n
Vector 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\n

equals() 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
\n\n

Summary

\n

The 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.

\n\n

Java Vector Java Vector

← Java Vector GetJava Vector Elements β†’