ArrayList 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.

Also read – ArrayList in java

More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e).

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