Check Existence Of An Element In Java ArrayList - Tutorialspoint
Maybe your like
- Home
- Whiteboard
- Online Compilers
- Practice
- Articles
- AI Assistant
- Jobs
- Tools
- Corporate Training
- Courses
- Certifications
- Switch theme
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The java.util.ArrayList.contains() method can be used to check if an element exists in an ArrayList or not. This method has a single parameter i.e. the element whose presence in the ArrayList is tested. Also it returns true if the element is present in the ArrayList and false if the element is not present.
A program that demonstrates this is given as follows
Example
Live Demo
import java.util.ArrayList;import java.util.List;publicclassDemo{ publicstaticvoid main(String[] args){ List aList =newArrayList(); aList.add("A"); aList.add("B"); aList.add("C"); aList.add("D"); aList.add("E"); System.out.println("The element C is available in ArrayList? "+ aList.contains("C")); System.out.println("The element Z is available in ArrayList? "+ aList.contains("Z")); }}Output
The output of the above program is as follows
The element C is available in ArrayList? true The element Z is available in ArrayList? falseNow let us understand the above program.
The ArrayList aList is created. Then ArrayList.add() is used to add the elements to the ArrayList. ArrayList.contains() is used to check if "C" and "Z" are available in the ArrayList and the result is displayed. A code snippet which demonstrates this is as follows
List aList = new ArrayList(); aList.add("A"); aList.add("B"); aList.add("C"); aList.add("D"); aList.add("E"); System.out.println("The element C is available in ArrayList? " + aList.contains("C")); System.out.println("The element Z is available in ArrayList? " + aList.contains("Z"));
George John Updated on: 2023-09-13T13:15:56+05:30 34K+ Views
Kickstart Your Career
Get certified by completing the course
Get StartedTag » Add Element If Not In List Java
-
Add Item To Arraylist If It Does Not Already Exist In List - Stack Overflow
-
Java ArrayList Contains() - Check If Element Exists - HowToDoInJava
-
Java 8 Check Is List Contains Else Add Code Example
-
How To Check Whether Element Exists In Java ArrayList?
-
How To Find An Element In A List With Java - Baeldung
-
ArrayList (Java Platform SE 8 ) - Oracle Help Center
-
Add Element At The Beginning Of A List In Java - Techie Delight
-
How To Check If ArrayList Contains An Item In Java? - Tutorialspoint
-
Append() Vs Extend() Vs Insert() In Python Lists - Stack Abuse
-
How To Add Optional To List Only If Present And Exists In Java
-
What Is The ntains() Method In Java?
-
ArrayList Methods, Sorting, Traversing In Java - ZetCode
-
Java List Add() And AddAll() Methods - JournalDev
-
How To Create, Initialize & Use List In Java - Software Testing Help
-
Array.push() Element If Does Not Exist Using JavaScript | Bobbyhadz
-
Java List - Tutorials Jenkov
-
List
.Remove(T) Method (System.Collections.Generic) -
9.11. Adding Values To A List — AP CSA Java Review - Obsolete