Arraylist contains() method: In the previous article we have discussed about Java ArrayList get() Method with Example
In this article we are going to see the use ArrayList contains() method along with suitable examples by using Java programming language.
Java ArrayList contains() Method with Example
contains( ):
Java arraylist contains: This java.util.ArrayList.contains()
method is used to check if the specified element in the arraylist is present or not.
It returns true if that specific element is present else it returns false
Syntax:
arrayListName.contains()
Where,
arrayListName
refers to the name of your ArrayList.
Let’s see different examples to understand it more clearly.
- Example with String Type ArrayList
- Example with Integer Type ArrayList
Method-1: Java ArrayList contains() Method – Example with String Type ArrayList
Approach:
- Create a new ArrayList of type String.
- Add string elements into the ArrayList using the add() method.
- Display the ArrayList elements.
- Now, using the
contains( )
method you can check if the desired element is present or not in the arrayList. - Print the result as true/false.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of string datatype ArrayList<String> arr = new ArrayList<String>(); // Adding some elements to the ArrayList arr.add("Hello"); arr.add("this"); arr.add("is"); arr.add("an"); arr.add("example of contains() method"); // Prints the ArrayList elements System.out.println("The elements of ArrayList are: "+arr); // checking specific elements in the arrayList System.out.println("Is Hello present in the arraylist: "+arr.contains("Hello")); System.out.println("Is hello present in the arraylist: "+arr.contains("hello")); } }
Output: The elements of ArrayList are: [Hello, this, is, an, example of contains() method] Is Hello present in the arraylist: true Is hello present in the arraylist: false
Method-2: Java ArrayList contains() Method – Example with Integer Type ArrayList
Approach:
- Create a new ArrayList of type Integer.
- Add Integer elements into the ArrayList using the add() method.
- Display the ArrayList elements.
- Now, using the contains( ) method you can check if the desired element is present or not in the arrayList.
- Print the result as true/false.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of integer datatype ArrayList<Integer> arr = new ArrayList<Integer>(); // Adding some elements to the ArrayList arr.add(10); arr.add(9); arr.add(8); arr.add(7); arr.add(6); // Prints the ArrayList elements System.out.println("The elements of ArrayList are: "+arr); // checking specific elements in the arrayList System.out.println("Is 9 present in the arraylist: "+arr.contains(9)); System.out.println("Is 99 present in the arraylist: "+arr.contains(99)); } }
Output: The elements of ArrayList are: [10, 9, 8, 7, 6] Is 9 present in the arraylist: true Is 99 present in the arraylist: false
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Related Java Programs: