Java ArrayList contains() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, contains(o) returns true if an ArrayList has at least one element equal to o, and false otherwise.

Equality is checked with equals(), not ==, so two different String objects with the same text match. contains walks the list from the start until it finds a match, so on a big list it is a slow linear search; a HashSet is the better tool for frequent lookups.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

Pass the value to look for.

Java
List<String> list = new ArrayList<>(Arrays.asList("red", "green"));
boolean a = list.contains("red");     // true
boolean b = list.contains("Red");     // false (case-sensitive)
boolean c = list.contains(null);      // false (no null stored)
CallWhat it does
list.contains(o)true if some element equals o.
if (!list.contains(o)) list.add(o)Adds a value only if it is not there yet.
list.indexOf(o) >= 0Same answer, written with indexOf.
set.contains(o)Much faster lookups in a HashSet.

contains(o) gives the same answer as indexOf(o) >= 0 and takes time proportional to the list size.

Library declarations
Java
// java.util.ArrayList<E>
public boolean contains(Object o)

// Declared in Collection<E> and List<E>

Parameters

ParameterDescription
o (Object)The value to look for. May be null and may be of any type.

Return Value

Return ValueDescription
booleantrue if at least one element equals o; otherwise false.

Compare with Similar Methods

Searching a list:

MethodHow it differs
indexOf(o)Also tells you where the first match is, or -1.
lastIndexOf(o)Finds the position of the last match.
remove(o)Finds the first match and removes it in one step.
retainAll(c)Keeps only the elements that another collection contains.

Java Examples

Example 1 — Check a Guest List

Look up three names, one of them with different capitalization.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListContainsDemo {
    public static void main(String[] args) {
        List<String> guests = new ArrayList<>(Arrays.asList("Nora", "Paul", "Rita"));

        System.out.println("Paul invited? " + guests.contains("Paul"));
        System.out.println("paul invited? " + guests.contains("paul"));
        System.out.println("Sam invited? " + guests.contains("Sam"));
    }
}

How It Works

  1. Exact match. "Paul" equals one of the elements, so contains returns true.
  2. Case counts. String.equals is case-sensitive, so "paul" with a small p is not found.
  3. Missing name. "Sam" was never added, so the search reaches the end of the list and returns false.
  4. Read only. None of the three lookups changes the guest list.

Example 2 — Add Only If Not Already Present

Record each visited page once, then do a case-insensitive check with a small helper.

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

public class ArrayListContainsUnique {
    public static void main(String[] args) {
        String[] clicks = {"home", "shop", "home", "cart", "shop"};
        List<String> visited = new ArrayList<>();

        for (String page : clicks) {
            if (!visited.contains(page)) {
                visited.add(page);
            }
        }
        System.out.println("unique pages: " + visited);
        System.out.println("case-insensitive check: " + containsIgnoreCase(visited, "CART"));
    }

    static boolean containsIgnoreCase(List<String> list, String target) {
        for (String s : list) {
            if (s.equalsIgnoreCase(target)) {
                return true;
            }
        }
        return false;
    }
}

How It Works

  1. Guard before adding. !visited.contains(page) is true only the first time a page name appears.
  2. First-seen order. home, shop, and cart are kept in the order they were first clicked; the repeats are skipped.
  3. No built-in ignore-case. contains always uses equals, so a small helper loops with equalsIgnoreCase instead.
  4. Found. "CART" matches "cart" when case is ignored, so the helper returns true.

Example 3 — contains() with Your Own Class Needs equals()

Two point classes, one without and one with an equals override.

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

public class ArrayListContainsEquals {
    static class Point {
        final int x, y;
        Point(int x, int y) { this.x = x; this.y = y; }
    }

    static class FixedPoint {
        final int x, y;
        FixedPoint(int x, int y) { this.x = x; this.y = y; }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof FixedPoint)) return false;
            FixedPoint p = (FixedPoint) o;
            return x == p.x && y == p.y;
        }

        @Override
        public int hashCode() {
            return 31 * x + y;
        }
    }

    public static void main(String[] args) {
        List<Point> plain = new ArrayList<>();
        plain.add(new Point(1, 2));
        System.out.println("without equals: " + plain.contains(new Point(1, 2)));

        List<FixedPoint> fixed = new ArrayList<>();
        fixed.add(new FixedPoint(1, 2));
        System.out.println("with equals:    " + fixed.contains(new FixedPoint(1, 2)));
    }
}

How It Works

  1. Equal-looking points. Each list holds a point at (1, 2), and each search uses a brand-new object with the same coordinates.
  2. Default equals. Point does not override equals, so it falls back to ==, and two separate objects are never ==. Result: false.
  3. Value equality. FixedPoint compares x and y in its own equals, so the new object matches the stored one.
  4. hashCode too. contains does not need hashCode, but overriding it together with equals keeps the class correct in a HashSet or HashMap.

Frequently Asked Questions

With equals(). It returns true as soon as it finds an element for which o.equals(element) is true (or both are null).
Yes, because String.equals is case-sensitive. Loop with equalsIgnoreCase for a case-insensitive check.
Yes. list.contains(null) returns true if the list holds a null element.
No, it checks the elements one by one, so the time grows with the list size. Use a HashSet for many lookups.
Your class probably does not override equals(), so only the identical object matches. Override equals (and hashCode) to compare by value.

Next: ensureCapacity()

Reserve room before adding many elements.

ensureCapacity() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful