Java ArrayList Contains example | Java ArrayList

Following ArrayList contains method programs find the given element is present in the ArrayList or not. Here return type is boolean. Returns true neither given element is present in the ArrayList nor false. ArrayList Contains method has been implemented from Collection Interface and overridden from AbstractCollection.

Arraylistcontains.java

import java.util.ArrayList;
public class ArrayListcontains
{
 public static void main(String[] args)
 {
  ArrayList & lt; Integer & gt; al = new ArrayList & lt; Integer & gt;();
  al.add(23);
  al.add(47);
  al.add(72);
  al.add(10);
  al.add(7);
  al.add(92);
  al.add(58);
  al.add(69);
  al.add(92);
  System.out.println(al);
  boolean a = al.contains(92);
  System.out.println(a);
  boolean b = al.contains(01);
  System.out.println(b);
 }
}

Output:

[23, 47, 72, 10, 7, 92, 58, 69, 92]

true

false

Leave a Reply

Your email address will not be published. Required fields are marked *