The arraylist contains() method is used to check if a specific value exists in an ArrayList or not. In this post, we will learn how to use ArrayList contains()
method with examples.
ArrayList contains() Method
Below is the syntax of the contains() method, defined in ArrayList class:
public boolean contains(Object o)
This method takes one object as its parameter. It checks if this object is in the ArrayList or not.It returns one boolean value. If the ArrayList contains at least one element, then it returns true. Else it returns false. It tries to find out at least one element v such that (o == null ? v == null : o.equals(v)
). i.e. if o
is null
, it returns true only if one element is null
in the ArrayList. If o is not null
, it returns true only if at least one element equal to v.
1. ArrayList contains() Example
Let’s look at the below example:
import java.util.ArrayList;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
ArrayList<String> arrList = new ArrayList<>(Arrays.asList("Mon", "Tues", "Wed"));
if (arrList.contains("Thurs")) {
System.out.println("Thurs is in the arraylist");
} else {
System.out.println("Thurs is not in the arraylist");
}
if (arrList.contains("Tues")) {
System.out.println("Tues is in the arraylist");
} else {
System.out.println("Tues is not in the arraylist");
}
}
}
We have created one ArrayList of strings in this example. The first if-else
block checks if the string “Thurs
” is in that ArrayList, and the second if-else block checks if “Tues
” is in the ArrayList. If you run this program, it will print the below output:
Thurs is not in the arraylist
Tues is in the arraylist
We can use ArrayList contains()
method that holds any type of data. For example, the below example shows how it works with an ArrayList of integers:
import java.util.ArrayList;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
ArrayList<Integer> arrList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
if (arrList.contains(5)) {
System.out.println("5 is in the arraylist");
} else {
System.out.println("5 is not in the arraylist");
}
}
}
The output will be same as the previous example. As mentioned before, contains can check for null values in an ArrayList. For example:
import java.util.ArrayList;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
ArrayList<Integer> arrList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, null));
if (arrList.contains(null)) {
System.out.println("null is in the arraylist");
} else {
System.out.println("null is not in the arraylist");
}
}
}
This program is checking for null values in the ArrayList. It will print “null is in the arraylist” since we have one null value. This method comes in handy to check for null values quickly in an ArrayList.
2. ArrayList indexOf() example
The contains method is useful to check if any specific element is available in an ArrayList or not. But if we want the index of the first occurrence of that element in the ArrayList, we can use indexOf
. We can also use this method to check if any value is available or not in an ArrayList. We define it as below:
public int indexOf(Object o)
It takes one parameter, which is the element to search for. The return value is an integer. It returns -1
if we find no element equal to the provided element. Else, it returns the index of the first occurrence of the specific element.Let’s try it with an example:
import java.util.ArrayList;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
ArrayList<Integer> arrList = new ArrayList<>(Arrays.asList(1, 2, 3, 1, 4, 5, 6, 7));
System.out.println(arrList.indexOf(1));
System.out.println(arrList.indexOf(10));
}
}
The first print statement is printing the index of 1
in the given ArrayList, and the second print statement is printing the index of 10 in that ArrayList. If you run it, it will print the below output:
0
-1
1 appears two times in the ArrayList, but it will return the index of its first occurrence, i.e. 0
. 10
is not in the ArrayList, so it returns -1
. If we need to check if an element is in an ArrayList or not and also its index, we can use indexOf
. -1
return value from this method is equal to false return from contains().
Summary
We learned how to use ArrayList contains()
method and indexOf
method of Java ArrayList in this post. We can use both methods to check if an element is in an ArrayList or not. indexOf also returns the index of an element, which makes it easier to get the position of an element if an ArrayList holds only unique values. The source code for these articles is available on our GitHub repository.