Introduction
The contains()
method in Java's ArrayList
is a handy way to check if an element already exists within your list. Think of it like looking through a box of toys – you want to quickly see if the toy car you’re holding is already in the box.
Syntax
public boolean contains(Object o)
Parameters
Parameter | Description |
---|---|
o |
The element you want to check for within the list. It can be any object, as long as it's properly compared for equality (using equals() method). |
Return Value
The contains()
method returns a boolean
value:
true
: If the element is found within the list.false
: If the element is not found in the list.
Examples
Example 1: Checking for a String
This example demonstrates how to use contains()
with a list of strings.
import java.util.ArrayList;
public class ContainsExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
String searchName = "Bob";
if (names.contains(searchName)) {
System.out.println(searchName + " is in the list.");
} else {
System.out.println(searchName + " is not in the list.");
}
}
Bob is in the list.
Explanation: We create an ArrayList
of strings called names
. We then use contains()
to see if “Bob” exists in the list. Since it does, the message “Bob is in the list.” is printed.
Example 2: Checking for an Integer
This example shows how to check for an integer within an ArrayList of Integers.
import java.util.ArrayList;
public class ContainsIntegerExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
int searchNumber = 25;
if (numbers.contains(searchNumber)) {
System.out.println(searchNumber + " is in the list.");
} else {
System.out.println(searchNumber + " is not in the list.");
}
}
25 is not in the list.
Explanation: We create an ArrayList
of integers named numbers
. We then try to find 25 using contains()
. Since 25 isn't present, we print “25 is not in the list.”
Example 3: Using a Custom Object
This example demonstrates how to use contains()
with objects of a custom class.
import java.util.ArrayList;
class Person {
String name;
public Person(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Person) {
return this.name.equals(((Person) obj).name);
} else {
return false;
}
}
@Override
public int hashCode(){
return name.hashCode(); //important for proper equals implementation
}
}
public class CustomObjectContainsExample {
public static void main(String[] args) {
ArrayList<Person> people = new ArrayList<>();
people.add(new Person("Alice"));
people.add(new Person("Bob"));
Person searchPerson = new Person("Charlie");
if (people.contains(searchPerson)) {
System.out.println(searchPerson.name + " is in the list.");
} else {
System.out.println(searchPerson.name + " is not in the list.");
}
}
Charlie is not in the list.
Explanation: Here, we're using a custom Person
class. Importantly, we’ve overridden the equals()
method to compare Person
objects based on their names. Without overriding equals, ArrayList would use object reference equality which is almost certainly not what you want when comparing objects with the same content.
Important Considerations
The contains()
method relies on the equals()
method of the objects being compared. Make sure that your custom classes have properly implemented equals()
and hashCode()
methods for accurate comparisons. If you don't, you might get unexpected results.