Java ArrayList.contains(Object o)

Syntax

ArrayList.contains(Object o) has the following syntax.

public boolean contains(Object o)

Example

In the following code shows how to use ArrayList.contains(Object o) method.


//from ww w  . ja  v  a 2  s  . c  o  m

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {

    ArrayList<Integer> arrlist = new ArrayList<Integer>();

    arrlist.add(20);
    arrlist.add(25);
    arrlist.add(10);
    arrlist.add(15);

    System.out.println(arrlist);

    // list contains element 10
    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");
    }

    // list does not contain element 30
    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");
    }
  }
}

The code above generates the following result.