java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to know if an ArrayList contains an item?

package com.javacoderanch.example.util;

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

public class ArrayListContains
{
    public static void main(String[] args)
    {
        ArrayList list = new ArrayList();
        list.add("Item 1");
        list.add("Item 2");

        if (list.contains("Item 1"))
        {
            System.out.println("True");
        }
        else
        {
            System.out.println("False");
        }
    }
}