Check if a particular element exists in ArrayList in Java

In this tutorial, we will learn how to Check if a particular element exists in ArrayList in Java.

ArrayList in Java is a resizable-array implementation of the List interface. ArrayList in Java implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. The ArrayList class extends AbstractList and implements the List interface.

To check if a particular element exists in ArrayList or not, ArrayList class in Java provides a method contains(). We will use this method to check if particular element exists in ArrayList. The syntax of this method is:

Syntax of contains() method:

Parameters:
obj – The element whose presence in this list is to be tested.

Returns:
true if the element exists in list.

Example:

 

Output:

 

Related posts

Leave a Comment