The ArrayList in Java is dynamic in nature that stores the elements without any limit. The elements can be added or removed whenever required. This data structure in Java is implemented using the java.util package. The first step involves declaring the object along with the data type. In the next step the elements are inserted into the ArrayList using the add() method. Like any other data structure, the ArrayList also contains different methods and one of the methods is the contains() method which is used to check the existence of a specific element in the targeted ArrayList.
This article will provide code examples to understand the ArrayList.contains() method in detail.
How to Use ArrayList.contains() Method in Java?
The ArrayList.contains() method in Java checks if the required string or the integer is available in the ArrayList or not. If the required element is present in the ArrayList, “true” is returned otherwise the contains() method returns a “false” that shows the unavailability of the element.
Syntax
The syntax of the contains() method is as below
arraylist.contains(Object obj)
In the above Syntax:
- The arraylist is the object for the ArrayList class in Java.
- The obj parameter is the required element from the ArrayList.
Return Value
The ArrayList.contains() method returns a boolean value as:
- “True” if the required element is present in the ArrayList.
- “False” if the required element is not available in the ArrayList.
The examples below discuss the implementation of the ArrayList.contains() method in Java.
Example 1: Basic Implementation of ArrayList.contains() Method
In the following coding example, the basic implementation of the ArrayList.contains() method will be utilized to check if an element “2” exists in the given ArrayList or not:
//Import the packages required
import java.util.ArrayList;
//Declare a new ContainExample1 class
class ContainExample1 {
public static void main(String[] args)
{
ArrayList<Integer> a1 = new ArrayList<Integer>();
//Use the add() method
a1.add(15);
a1.add(17);
a1.add(19);
System.out.println("The ArrayList is: " + a1);
//Check if the Integer is present or not
boolean output1 = a1.contains(2);
System.out.println("Does the ArrayList contains '2' " + output1);
//Check if the declared Integer is present or not
output1 = a1.contains(17);
System.out.println("Does the ArrayList contains '17' " + output1);
}
}
In this code block code:
- The “java.util.ArrayList” package is imported on the top.
- The class is declared as “ContainExample1”.
- In the main() method of Java, an object of “a1” is created for the Integer ArrayList.
- The strings are added utilizing the add() method.
- In the next part, the output is printed using the Boolean values that are either “true” or “false” according to the given value.
Output
In the output below, “false” is returned for the integer “2” and for the integer value of “17”, a true is returned since it is present in the ArrayList.

Example 2: Using ArrayList.contains() Method With Strings in Java
The code below shows how the contains() method checks whether the required string is present in the ArrayList or not.
//Import the packages required
import java.util.ArrayList;
//Declare the class
class ContainExample2 {
public static void main(String[] args)
{ //Create an ArrayList
ArrayList<String> arr1 = new ArrayList<String>();
//Use the add() method
arr1.add("Coding");
arr1.add("in");
arr1.add("Java");
arr1.add("is Fun");
System.out.println("The ArrayList is: " + arr1);
//Check if the string is present or not
boolean output1 = arr1.contains("Python");
if (output1)
System.out.println("This ArrayList contains Python");
else
System.out.println(
"This ArrayList does not contain Python");
//Check if the declared string is present or not
output1 = arr1.contains("Java");
if (output1)
System.out.println("This ArrayList contains Java");
else
System.out.println(
"The ArrayList does not contain Java");
}
}
In this Java code:
- The “java.util.ArrayList” package is imported in Java.
- The class is declared as “ContainExample2”.
- In the main() method of Java, an object of “arr1 is created for the ArrayList.
- The strings are added by executing the add() method.
- In the next part, the if-else statement checks for the availability of the required string from the specified string using the contains() method.
- In either case, the output is printed accordingly.
Output
The output below shows that “Python” is not present in the declared ArrayList whereas, “Java” is present.

Example 3: Using ArrayList.contains() Method on Integers in Java
The code below shows the working of the contains() method of an ArrayList on Integer values.
//Import the packages required
import java.util.ArrayList;
class ContainExample3 {
public static void main(String[] args)
{ ArrayList<Integer> a1 = new ArrayList<Integer>();
//Use the add() method
a1.add(15);
a1.add(17);
a1.add(19);
a1.add(22);
a1.add(25);
System.out.println("The ArrayList is: " + a1);
//Check if the Integer is present or not
boolean output1 = a1.contains(2);
if (output1)
System.out.println("This ArrayList contains '2'");
else
System.out.println(
"This ArrayList does not contain '2'");
//Check if the declared Integer is present or not
output1 = a1.contains(17);
if (output1)
System.out.println("This ArrayList contains '17'");
else
System.out.println(
"The ArrayList does not contain '17'");
}
}
In the above code block:
- The integer Arraylist is declared.
- The integers are added to the ArrayList by utilizing the Java’s add() method.
- In the next step, the contains() method checks for the availability of the integer in the ArrayList.
- The output is printed accordingly.
Output
The output below shows that the ArrayList contains the integer “17” while “2” is not part of the ArrayList.

Bonus Tip: Apart from the ArrayList, the contains() method can also be implemented on the strings as shown below.
Example: Check a Specific Case Sensitive String in the Declared String
The code below shows that the contains() method checks the UpperCase or the LowerCase in the declared string.
class Example4 {
public static void main(String args[]) {
String s1 = "HELLO IN JAVA";
System.out.println("The output is " + (s1.contains(s1.toUpperCase())));
String s2 = "code in Java";
System.out.println("The output is " + (s2.contains(s2.toUpperCase())));
}
}
In the above code block:
- The string is declared as “s1” in UpperCase and “s2” in LowerCase in Java.
- In the next step, the contains() method checks if the declared string is in UpperCase or not using the toUppercase() method.
- The output is printed accordingly.
Output
The output below shows that the first output is printed as “true” whereas the second output is printed as “false”.

This concludes the working of the ArrayList contains() method in Java.
Conclusion
In Java, the ArrayList.contains() method searches for the required element and retrieves a boolean value of “true” if the desired element is present; else it retrieves “false”. This article has effectively elaborated the implementation of the ArrayList.contains() method on the ArrayList of Strings and Integers in Java.