
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
Check If ArrayList Contains an Item in Java
To check if an ArrayList contains a specific item, we can either compare each element with the given item and print the result if they are equal, or use a predefined method that directly checks for the containing element.
ArrayList in Java
In Java, an ArrayList is a class similar to an Array, but unlike an array, it has a dynamic size that automatically increases or decreases, depending on the number of elements added or removed from it. Following is the syntax for creating an ArrayList in Java -
ArrayList<DataType> list_name = new ArrayList<>();
Here,
- DataType: It defines which type of element an ArrayList can accept.
- list_name: The name of the ArrayList.
Following are the ways for checking if the ArrayList contains a given item -
Checking an ArrayList item using the contains() Method
The contains() method of the ArrayList class accepts an item (element) as a parameter and verifies whether the given element is present in the current ArrayList. If the current ArrayList contains a given item, this method returns true; else, this method returns false.
Example
In the following example, we use the ArrayList contains() method to check whether the given item "C" is present in this list "{A, B, C, D}":
import java.util.ArrayList; public class CheckArrayListItem{ public static void main(String[] args){ //creating ArrayList type character ArrayList<Character> char_list = new ArrayList<>(); //adding item to it char_list.add('A'); char_list.add('B'); char_list.add('C'); char_list.add('D'); System.out.print("An ArrayList items are: "); for(Character c : char_list){ System.out.print(c + " "); } char ch = 'C'; System.err.println("\nThe given item is: " + ch); //using contains() method if(char_list.contains(ch)){ System.out.println("Yes! ArrayList contains the item '" + ch + "'"); } else{ System.out.println("No! ArrayList does not contain the item '" + ch + "'"); } } }
The above program produces the following output -
An ArrayList items are: A B C D The given item is: C Yes! ArrayList contains the item 'C'
Using an indexOf() Method
Another way to check if an ArrayList contains a given item is by using the indexOf() method. This method returns the index of the first occurrence of the specified element in the current list, or -1 if this list does not contain the specified element. If the result of it is not equal to -1, it means the element is present in the list.
Example
The following example uses the ArrayList.indexOf() method to check if the list {"Apple", "Banana", "Orange", "Grapes"} contains the given item "Orange":
import java.util.ArrayList; public class CheckArrayListItem{ public static void main(String[] args){ //creating ArrayList type String ArrayList<String> fruit_list = new ArrayList<>(); //adding item to it fruit_list.add("Apple"); fruit_list.add("Banana"); fruit_list.add("Orange"); fruit_list.add("Grapes"); System.out.print("An ArrayList items are: "); for(String s : fruit_list){ System.out.print(s + " "); } String fruit = "Orange"; System.err.println("\nThe item needs to check: " + fruit); //using indexOf() method int result = fruit_list.indexOf(fruit); if(result != -1){ System.out.println("Yes! ArrayList contains item " + fruit); } else{ System.out.println("No! ArrayList contains item " + fruit); } } }
Following is the output of the above program -
An ArrayList items are: Apple Banana Orange Grapes
The item needs to check: Orange
Yes! ArrayList contains item Orange
Using Comparison ("==") Operator
The get() method of the ArrayList class accepts an index value as a parameter and returns the element at the given position.
To check if a given item is present in an ArrayList,
- We need to iterate through the list using a for loop, retrieve each element using the get() method.
- Compare each element with the given item.
- If the current item is equal to the given item, the ArrayList contains the given item.
Example
In the example below, we iterate through the list using the get() method and "compare" it with the given "20" item to check whether the "ArrayList" {10, 20, 30, 40} contains the given item or not:
import java.util.ArrayList; public class CheckArrayListItem{ public static void main(String[] args){ //creating ArrayList type Integer ArrayList<Integer> num_list = new ArrayList<>(); //adding item to it num_list.add(10); num_list.add(20); num_list.add(30); num_list.add(40); System.out.print("An ArrayList items are: "); for(Integer n : num_list){ System.out.print(n + " "); } int num = 20; System.err.println("\nThe item needs to check: " + num); boolean isContain = true; //using for loop for(int i = 0; i<num_list.size(); i++){ //comparing items if(num_list.get(i) == num){ isContain = true; break; } else{ isContain = false; } } if(isContain){ System.out.println("Yes! ArrayList contains the item " + num); } else{ System.out.println("No! ArrayList does not contain the item " + num); } } }
Following is the output of the above program:
ArrayList items are: 10 20 30 40 The item needs to check: 20 Yes! ArrayList contains the item 20