How to Check whether Element Exists in Java ArrayList? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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 Company Preparation A aktmishra143 Follow Improve Article Tags : Java Java-Collections Java-ArrayList Practice Tags : JavaJava-Collections Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like