What does the method contains(obj o) do in java?


The contains(Object) method of the java.util.ArrayList class returns true if this list contains the specified element.

Example

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      ArrayList<Integer> arrlist = new ArrayList<Integer<(8);
      arrlist.add(20);
      arrlist.add(25);
      arrlist.add(10);
      arrlist.add(15);
      for (Integer number : arrlist) {
         System.out.println("Number = " + number);
      }
      boolean retval = arrlist.contains(10);
      if (retval == true) {
         System.out.println("element 10 is contained in the list");
      } else {
         System.out.println("element 10 is not contained in the list");
      }
      boolean retval2 = arrlist.contains(30);
      if (retval2 == true) {
         System.out.println("element 30 is contained in the list");
      } else {
         System.out.println("element 30 is not contained in the list");
      }
   }
}

Output

Number = 20
Number = 25
Number = 10
Number = 15
element 10 is contained in the list
element 30 is not contained in the list

Updated on: 25-Feb-2020

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements