Detailed Explanation of the Java Vector contains() Method
contains The method is Vector A very useful method provided by the class, used to check whether the collection contains the specified element.
Syntax format
public boolean contains(Object o)
Parameter Description
o: The element to check for existence in the collection
Return Value
true: If the collection contains the specified elementfalse: If the collection does not contain the specified element
How the contains Method Works
contains The method iterates through Vector all elements and uses the equals method to compare each element with the target element for equality. When the first matching element is found, the method immediately returns true; if no match is found after iterating all elements, returns false.
Underlying Implementation
In Java's source code,Vector 's contains the method actually calls the indexOf method in the component:
Example
public
boolean
contains
(
Object
o
)
{
return
indexOf
(
o,
0
)
=
0
;
}
indexOf method searches for the element starting from the specified position; it returns its index if found, otherwise -1.
Usage examples
Let's look at a few practical examples to see contains how the method is used.
Example 1: Basic Usage
Example
Vector
<
String
colors
=
new
Vector
<>
(
);
colors.
add
(
"Red"
)
;
colors.
add
(
"Green"
)
;
colors.
add
(
"Blue"
)
;
System
.
out
.
println
(
colors.
contains
(
"Green"
)
)
;
// Output: true
System
.
out
.
println
(
colors.
contains
(
"Yellow"
)
)
;
// Output: false
Example 2: Custom Objects
When using custom objects, you need to correctly implement equals method, otherwise contains method may not work as expected.
Example
class
Person
{
String
name
;
int
age
;
public
Person
(
String
name,
int
age
)
{
this
.
name
=
name
;
this
.
age
=
age
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
||
getClass
(
)
!=
obj.
getClass
(
)
)
return
false
;
Person person
=
(
Person
)
obj
;
return
age
==
person.
age
&&
Objects.
equals
(
name, person.
name
)
;
}
@Override
public
int
hashCode
(
)
{
return
Objects.
hash
(
name, age
)
;
}
}
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
Vector
<
Person
people
=
new
Vector
<>
(
);
people.
add
(
new
Person
(
"Alice"
,
25
)
)
;
people.
add
(
new
Person
(
"Bob"
,
30
)
)
;
System
.
out
.
println
(
people.
contains
(
new
Person
(
"Alice"
,
25
)
)
)
;
// Output: true
System
.
out
.
println
(
people.
contains
(
new
Person
(
"Charlie"
,
35
)
)
)
;
// Output: false
}
}
Performance considerations
Because Vector is implemented based on arrays, so thecontains method's time complexity is O(n), i.e., linear time. This means that as Vector the number of elements increases, the search time increases linearly.
Optimization Suggestions
If contains checks are performed frequently and the collection is large, consider the following optimizations:
-
Use HashSet: If order does not matter and elements are unique, the
HashSet'scontainsmethod has an average time complexity of O(1).
Example
colors.add("Red");
colors.add("Green");
System.out.println(colors.contains("Green")); // Fast lookup
Sort first, then use binary search: If Vector is sorted, you can first use Collections.sort() to sort, then use Collections.binarySearch() to search, with a time complexity of O(log n).
Example
sortedColors.add("Blue");
sortedColors.add("Green");
sortedColors.add("Red");
Collections.sort(sortedColors);
int index = Collections.binarySearch(sortedColors, "Green");
System.out.println(index >= 0); // Output: true
Frequently Asked Questions
Q1: Is the contains method case-sensitive?
For strings,contains the method is case-sensitive because it relies on equals method in the component:
Example
Vector
<
String
words
=
new
Vector
<>
(
);
words.
add
(
"Hello"
)
;
System
.
out
.
println
(
words.
contains
(
"hello"
)
)
;
// Output: false
If a case-insensitive check is needed, you can implement it like this:
Example
boolean
containsIgnoreCase
(
Vector
<
String
vector,
String
target
)
{
for
(
String
s:
vector
)
{
if
(
s.
equalsIgnoreCase
(
target
)
)
{
return
true
;
}
}
return
false
;
}
Q2: Can the contains method check for null values?
Yes,Vector allows storing null values, and the contains method can check for null:
Example
Vector
<
String
items
=
new
Vector
<>
(
);
items.
add
(
null
)
;
System
.
out
.
println
(
items.
contains
(
null
)
)
;
// Output: true
Q3: What is the difference between the contains method and the indexOf method?
contains: Returns a boolean value, telling you only whether the element existsindexOf: Returns the index position of the element, or -1 if it does not exist
Example
Vector
<
String
fruits
=
new
Vector
<>
(
);
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
System
.
out
.
println
(
fruits.
contains
(
"Banana"
)
)
;
// Output: true
System
.
out
.
println
(
fruits.
indexOf
(
"Banana"
)
)
;
// Output: 1
Summary
Vector 's contains The method is a simple but powerful tool for checking whether a specific element exists in a collection. Understanding how it works and its performance characteristics is very important for writing efficient Java code. Remember the following points:
containsThe method relies on theequalsUses- For custom objects, you must correctly implement
equalsandhashCodeMethod containsThe method's time complexity is O(n); for large collections, more efficient data structures may need to be consideredVectoris thread-safe, but if thread safety is not needed,ArrayListmay be a better choice
By using properly contains method, you can manage and query data in the collection more effectively.
Java Vector