ArrayList contains() method in Java
In this article, we will see the contains()
method of ArrayList class in Java. This method is used to check if the list contains the element.
Introduction
ArrayList is an ordered collection and it allows duplicate elements. It is widely used to store and retrieve data.
We have seen a few methods of the ArrayList class before like add()
, addAll()
, remove()
, set()
, iterate()
and clear()
. Today we will see the contains()
method.
If we need to check if any element is present in the list or not, we can use the contains()
method from the ArrayList class in Java.
ArrayList contains()
method
It has a method signature as given below.
1
public boolean contains(Object o)
This method is used to check if the ArrayList contains the specified element.
This method accepts a single parameter of type Object
o
: - an Objecto
whose presence in this list is to be tested
This method has a return type as a boolean
. It returns true
if the list contains the specified element. Otherwise, it returns false
.
I have given a Java program to check if the list contains the specified element using the ArrayList contains()
method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.ArrayList;
import java.util.List;
/**
* A Java program to check if the ArrayList
* contains the specified element.
*
* @author coderolls.com
*
*/
public class ArrayListContainsMethodExample {
public static void main(String[] args) {
//create an empty ArrayList object 'states'
List<String> states = new ArrayList<String>();
//add state in the ArrayList
states.add("California");
states.add("Texas");
states.add("Florida");
states.add("New Jersey");
states.add("Washington");
//check if states contain Florida
boolean isPresent1 = states.contains("Florida");
System.out.println("Is Florida present in the list: "+ isPresent1);
//check if states contain Alaska
boolean isPresent2 =states.contains("Alaska");
System.out.println("\nIs Alaska present in the list: "+ isPresent2);
}
}
Output:
1
2
3
Is Florida present in the list: true
Is Alaska present in the list: false
Explanation
- I have created an ArrayList object
states
. - I have added a few String objects in the
states
. - I have checked if the list
states
contains theFlorida
string object using thecontains()
method. SinceFlorida
is present in thestates
list, it returnstrue
. - I have captured the return value for the first
contains()
method in theisPresent1
boolean variable. When we print theisPresent1
, it has a valuetrue
. - Next time, I checked if the
states
list contains theAlaska
using thecontains()
method. SinceAlaska
is not present in thestates
list, it returnsfalse
- I have captured the return value for the second
contains()
method in theisPresent2
boolean variable. When we print theisPresent2
, it has a valuefalse
.
Conclusion
The ArrayList contains()
method is used to check if the list contains the specified element.
This method returns true
if the specified element is present in the list. Otherwise, it returns false
The example Java program used in the above article can be found at this GitHub repository.
If you know anything other than this, kindly write in the comment section below.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!