Java LinkedList contains() Method
contains() The method is in Java's LinkedList is a commonly used method provided by the class to check whether the list contains a specified element. This method belongs to java.util.LinkedList class, inherited from java.util.AbstractCollection class.
Method Syntax
public boolean contains(Object o)
Return Value
- If the list contains the specified element, it returns
true - If it does not contain the specified element, it returns
false
Method details
How It Works
contains() The method traverses each element in the list and uses equals() method to compare each element with the target object. If a matching element is found, it immediately returns true; if no matching element is found after traversing the entire list, it returns false.
Time Complexity
Because LinkedList is a doubly linked list,contains() method needs to start searching sequentially from the head of the list, so its time complexity is O(n), where n is the number of elements in the list.
Usage examples
Basic Usage
Example
import
java.util.LinkedList
;
public
class
LinkedListContainsExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a LinkedList
LinkedList
<
String
fruits
=
new
LinkedList
<>
(
);
// Add elements
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Orange"
)
;
// Check whether the element exists
System
.
out
.
println
(
"Contains Apple? "
fruits.
contains
(
"Apple"
)
)
;
// Output: true
System
.
out
.
println
(
"Contains Grape? "
fruits.
contains
(
"Grape"
)
)
;
// Output: false
}
}
Using custom objects
When using custom objects, you need to correctly override equals() method, otherwise contains() method may not work as expected.
Example
import
java.util.LinkedList
;
class
Person
{
private
String
name
;
private
int
age
;
public
Person
(
String
name,
int
age
)
{
this
.
name
=
name
;
this
.
age
=
age
;
}
// Override the equals method
@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
&&
name.
equals
(
person.
name
)
;
}
// Override the hashCode method
@Override
public
int
hashCode
(
)
{
return
31
name.
hashCode
(
)
age
;
}
}
public
class
CustomObjectExample
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
Person
people
=
new
LinkedList
<>
(
);
people.
add
(
new
Person
(
"Alice"
,
25
)
)
;
people.
add
(
new
Person
(
"Bob"
,
30
)
)
;
Person alice
=
new
Person
(
"Alice"
,
25
)
;
System
.
out
.
println
(
"Contains Alice? "
people.
contains
(
alice
)
)
;
// Output: true
}
}
Notes
1. Null value handling
LinkedList allows containing null value, you can use contains(null) to check whether the list contains null values.
Example
LinkedList
<
String
list
=
new
LinkedList
<>
(
);
list.
add
(
null
)
;
System
.
out
.
println
(
list.
contains
(
null
)
)
;
// Output: true
2. Performance Considerations
Because contains() method needs to traverse the list; for large lists, frequent calls to this method may affect performance. If you need to frequently check whether an element exists, consider using HashSet and other data structures that are more suitable for lookup.
3. The importance of the equals() method
contains() method relies on the element's equals() method for object comparison. If the custom class does not correctly override equals() method,contains() method may fail to correctly identify equal objects.
Comparison with Other Methods
contains() vs indexOf()
contains(): Returns boolean, only determines whether the element existsindexOf(): Returns int, returns the index of the first occurrence of the element, or -1 if it does not exist
Example
LinkedList
<
String
list
=
new
LinkedList
<>
(
);
list.
add
(
"A"
)
;
list.
add
(
"B"
)
;
list.
add
(
"A"
)
;
System
.
out
.
println
(
list.
contains
(
"A"
)
)
;
// Output: true
System
.
out
.
println
(
list.
indexOf
(
"A"
)
)
;
// Output: 0
contains() vs containsAll()
contains(): Checks whether a single element existscontainsAll(): Checks whether it contains all elements of a specified collection
Example
LinkedList
<
String
list
=
new
LinkedList
<>
(
);
list.
add
(
"A"
)
;
list.
add
(
"B"
)
;
list.
add
(
"C"
)
;
System
.
out
.
println
(
list.
contains
(
"A"
)
)
;
// Output: true
List
<
String
checkList
=
Arrays.
asList
(
"A"
,
"B"
)
;
System
.
out
.
println
(
list.
containsAll
(
checkList
)
)
;
// Output: true
Summary
LinkedList 's contains() method is a simple but practical tool for checking whether a specific element exists in a list. Understanding how it works and how to use it correctly is very important for writing efficient Java programs. Remember the following points:
contains()method usesequals()for element comparison- For custom objects, you must correctly override
equals()andhashCode()Method - This method has a time complexity of O(n) and is not suitable for frequent use on large lists
- You can choose based on specific needs
indexOf()orcontainsAll()and other alternative methods
By using properly contains() method, you can effectively manage and manipulate LinkedList the data in.
Java LinkedList