How do I know if an ArrayList contains a specified item?

In this example you are going to learn how to find out if a List or ArrayList object has a specified element. To check if an ArrayList object contains a specified element we can use the contains() method. This method returns a boolean true when the specified element is found in the ArrayList, if not it will return false.

package org.kodejava.util;

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

public class ArrayListContainsExample {
    private final List<String> list = new ArrayList<>();

    public ArrayListContainsExample() {
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");
        list.add("Item 4");

    public static void main(String[] args) {
        ArrayListContainsExample demo = new ArrayListContainsExample();

        // Check to see if the list contains "Item 1".
        String itemToFind = "Item 1";

        // Check to see if the list contains "Item 20".
        itemToFind = "Item 20";

    private void findItemInTheList(String itemToFind) {
        if (list.contains(itemToFind)) {
            System.out.println(itemToFind + " was found in the list");
        } else {
            System.out.println(itemToFind + " was not found in the list");

The output of the code snippet above are:

Item 1 was found in the list
Item 20 was not found in the list


    • Hi Amrendra,

      You need to implements the equals() method in your pojo so that the list.contains() method knows if the list already contains the same pojo. And when you implement the equals() method you will also need to implement the hashCode() method.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.