The contains()
method is one of the most commonly used methods in the Java ArrayList
class. It allows you to check whether a specific element exists in an ArrayList
or not. This method is essential when you need to verify the presence of an item before performing operations such as adding, removing, or updating elements.
What Is the contains() Method in ArrayList?
- The
contains()
method returnstrue
if the specified element is present in the list. - It returns
false
if the element is not found. - Internally, it uses the
equals()
method to compare elements, so for custom objects, ensure theequals()
method is properly overridden. - The method accepts an
Object
as a parameter and checks for its presence.
Syntax
public boolean contains(Object o)
Parameters
Parameter | Description |
---|---|
o | The element to be tested for presence in the list. |
Return Value
- Returns
true
if theArrayList
contains the specified element. - Returns
false
otherwise.
Exceptions
- The method does not throw any checked exceptions.
- If the element is
null
, it works correctly as it handles null elements internally. - If the list implementation does not support
contains()
, it may throwUnsupportedOperationException
(rare for standardArrayList
).
How Does contains() Work Internally?
contains()
iterates through all elements until it finds an element that is equal to the specified object (according to theequals()
method).- If an element matches, it immediately returns
true
. - If it completes the iteration without a match, it returns
false
. - Because of this linear search, the time complexity is O(n) in the worst case, where
n
is the size of the list.
Examples of the contains() Method
1. Check Presence of a String in ArrayList
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); System.out.println(cars.contains("BMW")); // true System.out.println(cars.contains("Toyota")); // false } }
Output:
true
false
2. Check Presence of an Integer in ArrayList
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(2); numbers.add(4); numbers.add(6); numbers.add(8); System.out.println(numbers.contains(4)); // true System.out.println(numbers.contains(5)); // false } }
3. Using contains() with Custom Objects
import java.util.ArrayList; class Person { String name; Person(String name) { this.name = name; } // Override equals() for correct contains() behavior @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Person)) return false; Person other = (Person) obj; return this.name.equals(other.name); } @Override public int hashCode() { return name.hashCode(); } } public class Demo { public static void main(String[] args) { ArrayList<Person> people = new ArrayList<>(); people.add(new Person("Alice")); people.add(new Person("Bob")); System.out.println(people.contains(new Person("Alice"))); // true System.out.println(people.contains(new Person("Carol"))); // false } }
Important Notes
- The efficacy of
contains()
depends on the proper implementation of theequals()
method for custom objects. - It performs a linear search, so avoid excessive use in very large lists for performance reasons.
- Use collections optimized for faster searches like
HashSet
if frequent contains checks are needed.
Summary
Aspect | Details |
---|---|
Method Purpose | Check if an element exists in an ArrayList |
Syntax | boolean contains(Object o) |
Parameter | Element to find in the list |
Returns | true if found; false otherwise |
Exceptions | None |
Time Complexity | O(n) — linear search |
Conclusion
The contains()
method in Java’s ArrayList
is a simple yet powerful way to verify the presence of elements. Whether you are working with primitive wrapper types like String
or Integer
, or custom objects, contains()
offers an easy, readable way to perform membership tests in a list. Always ensure your custom classes implement equals()
correctly when using this method.