Java LinkedList contains() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, contains(o) returns true if the LinkedList has at least one element equal to o, and false otherwise. The list is not changed.

Elements are compared with equals, and the search is linear: Java checks the elements one by one from the front. A null argument is allowed and matches a null element.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it.

Syntax

Pass the value to look for.

Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C"));
boolean a = list.contains("B");   // true
boolean b = list.contains("Z");   // false
CallWhat it does
list.contains(o)true if an equal element exists.
list.indexOf(o)Also gives the position, or -1.
list.containsAll(c)Checks a whole collection at once.
set.contains(o)The same question asked of a HashSet, which answers much faster.

contains only reads the list, so it never throws an exception for a normal value.

Library declarations
Java
// java.util.LinkedList
public boolean contains(Object o)

Parameters

ParameterDescription
o (Object)The element to look for. May be null.

Return Value

Return ValueDescription
booleantrue if the list contains at least one element equal to o.

Compare with Similar Methods

Ways to test for an element:

MethodHow it differs
indexOf(o)Also tells you where the first match is.
lastIndexOf(o)Tells you where the last match is.
retainAll(c)Keeps only the elements found in another collection.
HashSet.contains()The same question on a set, answered in near constant time.

Java Examples

Example 1 — Check Whether a Value Is in the List

Ask a list of allowed users whether several names are on it.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListContainsDemo {
    public static void main(String[] args) {
        LinkedList<String> allowed = new LinkedList<>(
            Arrays.asList("root", "alice", "bob"));

        System.out.println("alice: " + allowed.contains("alice"));
        System.out.println("carol: " + allowed.contains("carol"));
        System.out.println("Alice: " + allowed.contains("Alice"));

        String user = "bob";
        if (allowed.contains(user)) {
            System.out.println("welcome back, " + user);
        }
    }
}

How It Works

  1. Three allowed users. The list holds root, alice and bob.
  2. A present value. "alice" is in the list, so the answer is true.
  3. Missing and misspelled. "carol" is not there, and "Alice" with a capital A is a different string, so both give false.
  4. Used in an if. Because "bob" is in the list, the welcome message is printed.

Example 2 — Search for null in a List

Check whether a list holds a null element, before and after adding one.

Java
import java.util.LinkedList;

public class LinkedListContainsNull {
    public static void main(String[] args) {
        LinkedList<String> answers = new LinkedList<>();
        answers.add("yes");
        answers.add("no");
        System.out.println("has null: " + answers.contains(null));

        answers.add(null);
        System.out.println("has null: " + answers.contains(null));
        System.out.println("size: " + answers.size());
    }
}

How It Works

  1. Two real answers. No element is null yet.
  2. First check. contains(null) is allowed, and returns false.
  3. A null is stored. A LinkedList accepts null elements, so add(null) works.
  4. Second check. Now contains(null) returns true, and the size is 3.

Example 3 — Avoid the Integer and Long Trap

Check a list of Integer values with an int and with a long, and see why the results differ.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListContainsTrap {
    public static void main(String[] args) {
        LinkedList<Integer> ids = new LinkedList<>(Arrays.asList(5, 10, 15));

        System.out.println("contains(5):  " + ids.contains(5));
        System.out.println("contains(5L): " + ids.contains(5L));
        System.out.println("5L == 5:       " + (5L == 5));
        System.out.println("fixed: " + ids.contains((int) 5L));
    }
}

How It Works

  1. A list of Integer objects. The list holds 5, 10 and 15.
  2. An int matches. 5 is boxed to an Integer, which equals the stored 5.
  3. A long does not. 5L is boxed to a Long. A Long is never equal to an Integer, so the answer is false.
  4. The plain comparison is true. Primitive 5L == 5 compares numbers. Casting the long back with (int) makes contains find the value again.

Frequently Asked Questions

Call list.contains(value). It returns true if an equal element is in the list.
It uses equals. For your own classes, override equals to make contains work as you expect.
Yes. contains(null) returns true if the list holds a null element.
A common cause is a type mismatch, such as searching for a Long in a list of Integer values. The two types are never equal.
It scans the elements one by one, so the time grows with the size of the list. Use a HashSet for fast membership tests.

Next: descendingIterator()

Walk the list from the last element to the first.

descendingIterator() 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