Check if a Java ArrayList contains a given item or not
Java 8Object Oriented ProgrammingProgramming
The java.util.ArrayList.contains() method can be used to check if a Java ArrayList contains a given item or not. This method has a single parameter i.e. the item whose presence in the ArrayList is tested. Also it returns true if the item is present in the ArrayList and false if the item is not present.
A program that demonstrates this is given as follows −
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 forParameters:
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<Integer> arr = new ArrayList<Integer>(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<String> arr = new ArrayList<String>(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
ArrayList contains() Example
Let’s look at the program with an Integer type.
import java.util.ArrayList; public class Demo1 { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(12); numbers.add(43); numbers.add(54); numbers.add(23); numbers.add(89); numbers.add(76); System.out.println("ArrayList elements : " + numbers); System.out.println("Does the list have 54 : " + numbers.contains(54)); System.out.println("Does the list have 45 : " + numbers.contains(45)); } }Output:
ArrayList elements : [12, 43, 54, 23, 89, 76] Does the list have 54 : true Does the list have 45 : falseArrayList contains(Object o) method in java
Let’s learn ArrayList contains(Object o) method in java.
ArrayList contains(Object o) method in java
contains(Object o) method of ArrayList class returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e).
Syntax:
public boolean contains(Object o)
Parameters:
o element whose presence in this list is to be tested.
Returns:
true if this list contains the specified element.
Now let’s see example on ArrayList contains(Object o) method.
import java.util.ArrayList; public class ArrayListContainsMethodExample { public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(5); names.add("virat"); names.add("dhoni"); names.add("rohit"); names.add("vijay"); names.add("ajay"); System.out.println("Names: " + names); System.out.print("Does list contains name dhoni?: "); System.out.println(names.contains("dhoni")); System.out.print("Does list contains name Sudhakar?: "); System.out.println(names.contains("Sudhakar")); } }
Output:
Names: [virat, dhoni, rohit, vijay, ajay]
Does list contains name dhoni?: true
Does list contains name Sudhakar?: false
Also read – java overview
Posted in: JavaTagged: ArrayList contains(Object o) method in java
ArrayList contains() Method
Below is the syntax of the contains() method, defined in ArrayList class:
public boolean contains(Object o)This method takes one object as its parameter. It checks if this object is in the ArrayList or not.It returns one boolean value. If the ArrayList contains at least one element, then it returns true. Else it returns false. It tries to find out at least one element vsuch that (o == null ? v == null : o.equals(v)). i.e. if ois null,it returns trueonly if one element isnull in the ArrayList. If ois not null, it returns trueonly if at least one element equal to v.