How do you check if an ArrayList has a certain value?

1. ArrayList contains[] syntax

The contains[] method is pretty simple. It simply checks the index of element in the list. If the index is greater than '0' than element is present in the list.

public boolean contains[Object o] { return indexOf[o] >= 0; }

Arraylist.contains[] in Java

ArrayList contains[] method in Java is used for checking if the specified element exists in the given list or not.

Syntax:

public boolean contains[Object] object-element to be searched for

Parameters:
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.

Code#1: Demonstrate the working of the method contains[] in integer






// Java code to demonstrate the working of

// contains[] method in ArrayList

// for ArrayList functions

import java.util.ArrayList;

class GFG {

public static void main[String[] args]

{

// creating an Empty Integer ArrayList

ArrayList arr = new ArrayList[4];

// using add[] to initialize values

// [1, 2, 3, 4]

arr.add[1];

arr.add[2];

arr.add[3];

arr.add[4];

// use contains[] to check if the element

// 2 exits or not

boolean ans = arr.contains[2];

if [ans]

System.out.println["The list contains 2"];

else

System.out.println["The list does not contains 2"];

// use contains[] to check if the element

// 5 exits or not

ans = arr.contains[5];

if [ans]

System.out.println["The list contains 5"];

else

System.out.println["The list does not contains 5"];

}

}

Output: The list contains 2 The list does not contains 5

Code#2: Demonstrate the working of the method contains[] in string




// Java code to demonstrate the working of

// contains[] method in ArrayList of string

// for ArrayList functions

import java.util.ArrayList;

class GFG {

public static void main[String[] args]

{

// creating an Empty String ArrayList

ArrayList arr = new ArrayList[4];

// using add[] to initialize values

// ["geeks", "for", "geeks"]

arr.add["geeks"];

arr.add["for"];

arr.add["geeks"];

// use contains[] to check if the element

// "geeks" exits or not

boolean ans = arr.contains["geeks"];

if [ans]

System.out.println["The list contains geeks"];

else

System.out.println["The list does not contains geeks"];

// use contains[] to check if the element

// "coding" exits or not

ans = arr.contains["coding"];

if [ans]

System.out.println["The list contains coding"];

else

System.out.println["The list does not contains coding"];

}

}

Output: The list contains geeks The list does not contains coding

Practical Application:
In search operations, we can check if a given element exists in a list or not.

Reference:
Oracle Docs




Article Tags :

Java

Java - util package

Java-ArrayList

Java-Collections

java-list

Practice Tags :

Java

Java-Collections

Read Full Article

Check if ArrayList contains a Specific Object

To check if ArrayList contains a specific object or element, use ArrayList.contains[] method. You can call contains[] method on the ArrayList, with the element passed as argument to the method. contains[] method returns true if the object is present in the list, else the method returns false.

ArrayList.contains[] – Reference to Syntax and Examples.

Following is a quick code example to use ArrayList.contains[] method.

boolean b = arraylist_1.contains[element];

Example 1 – Check if Element is present in ArrayList

In the following program, we shall take an ArrayList of strings, and check if the string object “banana” is present in the list.

Java Program

import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main[String[] args] { ArrayList arraylist_1 = new ArrayList[ Arrays.asList["apple", "banana", "mango", "orange"]]; String element = "banana"; if[arraylist_1.contains[element]] { System.out.println[element+" is present in the list."]; } else { System.out.println[element+" is not present in the list."]; } } }

Output

banana is present in the list.

Example 2 – Check if Element is present in ArrayList – Negative Scenario

In the following program, we shall take an ArrayList of strings, and check if the string object “lemon” is present in the list.

Java Program

import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main[String[] args] { ArrayList arraylist_1 = new ArrayList[ Arrays.asList["apple", "banana", "mango", "orange"]]; String element = "lemon"; if[arraylist_1.contains[element]] { System.out.println[element+" is present in the list."]; } else { System.out.println[element+" is not present in the list."]; } } }

As the specified element is not present in the ArrayList, contains[] method returns false, and the else block executes.

Output

lemon is not present in the list.

Conclusion

In this Java Tutorial, we learned how to check if given element is present in the ArrayList or not, using contains[] method.

Java ArrayList.contains[]

To check if an ArrayList contains specified element in Java, call contains[] method on the given ArrayList and pass the element as argument to it.

In this tutorial, we will learn about the Java ArrayList.contains[] method, and learn how to use this method to check if this ArrayList contains specified element, with the help of examples.

Video liên quan

Bài mới nhất

Chủ Đề