Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector equals() Method

Java Vector equals() Method

Java Vector Java Vector

equals() The method is in Java's Vector An important method provided by the class, used to compare two Vector objects for equality. This method is inherited from the AbstractList class and implements the List interface's equals() method definition.

Syntax format


public boolean equals(Object o)

Method parameters

- Type: `Object` - Can be any Java object, but content comparison is performed only when it is of the `List` type

Return Value

- `true`: If the specified object is also a `List`, and both lists have the same size, and all corresponding element pairs are equal - `false`: All other cases

Method Characteristics

1. Content Comparison Rather Than Reference Comparison

equals() The method compares the Vector 's content rather than reference addresses. Even if two Vector are different object instances, as long as they contain the same elements in the same order,equals() will return true.

Example

Vector

<
String
v1
=
new
Vector
<>
(
Arrays

.

asList

(

"A"

,

"B"

,

"C"

)

)

;

Vector

<
String
v2
=
new
Vector
<>
(
Arrays

.

asList

(

"A"

,

"B"

,

"C"

)

)

;

System

.

out

.

println

(

v1.

equals

(

v2

)

)

;

// Outputs true

2. Compatible with the List Interface

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

Example

Vector

<
String
vector
=
new
Vector
<>
(
Arrays

.

asList

(

1

,

2

,

3

)

)

;

ArrayList

<
Integer
arrayList
=
new
ArrayList
<>
(
Arrays

.

asList

(

1

,

2

,

3

)

)

;

System

.

out

.

println

(

vector.

equals

(

arrayList

)

)

;

// Outputs true

3. Order Sensitive

equals() The method is sensitive to element order; even if the same elements are contained but in a different order, it returns false.

Example

Vector

<
String
v1
=
new
Vector
<>
(
Arrays

.

asList

(

"A"

,

"B"

,

"C"

)

)

;

Vector

<
String
v2
=
new
Vector
<>
(
Arrays

.

asList

(

"C"

,

"B"

,

"A"

)

)

;

System

.

out

.

println

(

v1.

equals

(

v2

)

)

;

// Outputs false


Implementation principle

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

  1. First checks whether the compared object is the same instance (reference equality)
  2. Checks whether the object is a List Type
  3. Compares whether the two lists have the same size
  4. Compares elements at corresponding positions one by one for equality (using the element's equals() method)

Notes

1. The equals() Method of Element Objects

Vector 's equals() The method relies on the equals() method implementation of its elements. If custom objects are stored, ensure that equals() method.

2. Null Value Handling

Vector can contain null elements,equals() The method can correctly handle null value comparisons.

Example

Vector

<
String
v1
=
new
Vector
<>
(
Arrays

.

asList

(

null

,

"B"

)

)

;

Vector

<
String
v2
=
new
Vector
<>
(
Arrays

.

asList

(

null

,

"B"

)

)

;

System

.

out

.

println

(

v1.

equals

(

v2

)

)

;

// Outputs true

3. Performance considerations

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


Example Code

Example

import

java.util.Vector

;

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 ==

Example

Vector

<
String
v1
=
new
Vector
<>
(
)

;

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()


Summary

Vector 's equals() The method provides content-level comparison and is the standard way to determine whether two Vector are equal in content. Understanding how it works and its characteristics is very important for correctly using the Vector collection. In actual programming, choose an appropriate comparison method based on specific needs, and pay attention to custom objects' equals() method implementation.

Java Vector Java Vector