How to Check whether Element Exists in Java ArrayList? Last Updated : 18 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Java ArrayList is a resizable array, which can be found in java.util package. We can add or delete elements from an ArrayList whenever we want, unlike a built-in array. We can check whether an element exists in ArrayList in java in two ways: Using contains() methodUsing indexOf() method Method 1: Using contains() method The contains() method of the java.util.ArrayList class can be used to check whether an element exists in Java ArrayList. Syntax: public boolean contains(Object) Parameter: object - element whose presence in this list is to be tested Returns: It returns true if the specified element is found in the list else it returns false. Java // Java program to check // whether element exists // in Java ArrayList import java.io.*; import java.util.ArrayList; class GFG { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); // use add() method to add elements in the list list.add(1); list.add(2); list.add(3); list.add(4); // passing 5 as a // parameter to contains() // function if (list.contains(5)) System.out.println("5 exists in the ArrayList"); else System.out.println("5 does not exist in the ArrayList"); if (list.contains(2)) System.out.println("2 exists in the ArrayList"); else System.out.println("2 does not exist in the ArrayList"); } } Output5 does not exist in the ArrayList 2 exists in the ArrayList Method 2: Using indexOf() method Contains() method uses indexOf() method to determine if a specified element is present in the list or not. So we can also directly use the indexOf() method to check the existence of any supplied element value. Java // Java program to check // whether element exists // in Java ArrayList import java.io.*; import java.util.ArrayList; class GFG { public static void main (String[] args) { ArrayList<Integer> list = new ArrayList<>(); // use add() method to add elements in the list list.add(2); list.add(5); list.add(1); list.add(6); // passing 5 as a // parameter to contains() // function if(list.indexOf(5)>=0) System.out.println("5 exists in the ArrayList"); else System.out.println("5 does not exist in the ArrayList"); if(list.indexOf(8)>=0) System.out.println("8 exists in the ArrayList"); else System.out.println("8 does not exist in the ArrayList"); } } Output5 exists in the ArrayList 8 does not exist in the ArrayList Comment More infoAdvertise with us Next Article How to Check whether Element Exists in Java ArrayList? A aktmishra143 Follow Improve Article Tags : Java Java-Collections Java-ArrayList Practice Tags : JavaJava-Collections Similar Reads Check whether array has all identical elements using Arrays.asList() and HashSet in Java Given an array arr[] of N elements, the task is to check whether the array have all same (identical) elements or not without using the loop. If all elements are same then print Yes otherwise print No. Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: Yes The given array has all identical elements i.e 2 min read ArrayList isEmpty() Method in Java with Examples In Java, the isEmpty() method of ArrayList is used to check if an ArrayList is empty.Example 1: Here, we use the isEmpty() method to check whether an ArrayList of integers is empty.Java// Java program to demonstrate the use of isEmpty() // method with ArrayList of Integers import java.util.ArrayList 2 min read How to check if a key exists in a HashMap in Java Given a HashMap and a key in Java, the task is to check if this key exists in the HashMap or not. Examples: Input: HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}, key = 2 Output: true Input: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, key = 10 Output: false Using Iterator (Not Efficient): Get the HashMap a 4 min read List add(int index, E element) method in Java The add(int index, E ele) method of List interface in Java is used to insert the specified element at the given index in the current list. Implementation:Java// Java code to illustrate add(int index, E elements) import java.util.*; public class ArrayListDemo { public static void main(String[] args) 2 min read How to check if an Array contains a value or not? There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-b 3 min read Like