How to check if ArrayList contains an item in Java?

You can utilize contains() method of the List interface to check if an object is present in the list.

contains() method

boolean contains(Object o)

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 (o==null ? e==null : o.equals(e)).


  • c − Element whose presence in this list is to be tested.


True if this list contains the specified element.


  • ClassCastException − If the type of the specified element is incompatible with this list (optional).

  • NullPointerException − If the specified element is null and this list does not permit null elements (optional).


Following is the example showing the usage of contains() methods −

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

public class CollectionsDemo {
   public static void main(String[] args) {
      List list = new ArrayList<>();
      list.add(new Student(1, "Zara"));
      list.add(new Student(2, "Mahnaz"));
      list.add(new Student(3, "Ayan"));
      System.out.println("List: " + list);
      Student student = new Student(3, "Ayan");
      if(list.contains(student)) {
         System.out.println("Ayan is present.");
class Student {
   private int id;
   private String name;
   public Student(int id, String name) { = id; = name;
   public int getId() {
      return id;
   public void setId(int id) { = id;
   public String getName() {
      return name;
   public void setName(String name) { = name;
   public boolean equals(Object obj) {
      if(!(obj instanceof Student)) {
         return false;
      Student student = (Student)obj;
      return == student.getId() &&;
   public String toString() {
      return "[" + + "," + + "]";


This will produce the following result −

Note: com/tutorialspoint/ uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
List: [[1,Zara], [2,Mahnaz], [3,Ayan]]
Ayan is present.