Java LinkedList indexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, indexOf(o) searches the LinkedList from the front and returns the index of the first element that is equal to o. If no element matches, it returns -1.

Elements are compared with equals, so for your own classes you should override equals to get useful results. 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", "B"));
int a = list.indexOf("B");   // 1, the first B
int b = list.indexOf("Z");   // -1, not found
CallWhat it does
list.indexOf(o)Index of the first match, or -1.
list.lastIndexOf(o)Index of the last match, or -1.
list.contains(o)Only tells whether a match exists.
list.get(i)The opposite direction: from index to element.

list.indexOf(x) >= 0 is the same test as list.contains(x).

Library declarations
Java
// java.util.LinkedList
public int indexOf(Object o)

Parameters

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

Return Value

Return ValueDescription
intThe index of the first match, or -1 if the list does not contain o.

Compare with Similar Methods

Ways to search a list:

MethodHow it differs
lastIndexOf(o)Finds the last match instead of the first.
contains(o)Returns just true or false.
get(index)Goes from an index to an element.
ArrayList.indexOf()The same method on an array-backed list.

Java Examples

Example 1 — Find Where a Value Sits in a List

Search for a name that appears once, one that appears twice, and one that is missing.

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

public class LinkedListIndexOfDemo {
    public static void main(String[] args) {
        LinkedList<String> team = new LinkedList<>(
            Arrays.asList("Ira", "Jon", "Kim", "Jon", "Lee"));

        System.out.println("Kim: " + team.indexOf("Kim"));
        System.out.println("Jon: " + team.indexOf("Jon"));
        System.out.println("Zed: " + team.indexOf("Zed"));
        System.out.println("jon: " + team.indexOf("jon"));
    }
}

How It Works

  1. Kim is unique. The search finds her at index 2.
  2. Jon appears twice. indexOf stops at the first match, index 1, and ignores the one at index 3.
  3. No match gives -1. "Zed" is not in the list.
  4. Case matters. "jon" with a lower-case j is a different string from "Jon", so the result is -1 again.

Example 2 — Use indexOf Safely Before get

Look up an item and only use the index when it is not -1.

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

public class LinkedListIndexOfSafe {
    public static void main(String[] args) {
        LinkedList<String> queue = new LinkedList<>(
            Arrays.asList("task-1", "task-2", "task-3"));

        String[] wanted = {"task-3", "task-9"};
        for (String id : wanted) {
            int pos = queue.indexOf(id);
            if (pos >= 0) {
                System.out.println(id + " is at " + pos + ", ahead of it: " + pos);
            } else {
                System.out.println(id + " is not in the queue");
            }
        }

        System.out.println("null index: " + queue.indexOf(null));
    }
}

How It Works

  1. A queue of tasks. The three tasks sit at indexes 0, 1 and 2.
  2. Test the result. An index of 0 or more means the task was found, and -1 means it was not.
  3. task-3 is found. It is at index 2, which also means that two tasks are ahead of it.
  4. task-9 and null are missing. The first gives the not-found message, and searching for null returns -1 because no element is null.

Example 3 — Search for Objects with equals

Search for a different object that is equal to one in the list, because the class overrides equals.

Java
import java.util.LinkedList;

public class LinkedListIndexOfEquals {
    static class Tag {
        final String label;

        Tag(String label) {
            this.label = label;
        }

        @Override
        public boolean equals(Object other) {
            return other instanceof Tag && ((Tag) other).label.equals(label);
        }

        @Override
        public int hashCode() {
            return label.hashCode();
        }
    }

    public static void main(String[] args) {
        LinkedList<Tag> tags = new LinkedList<>();
        tags.add(new Tag("java"));
        tags.add(new Tag("sql"));

        Tag probe = new Tag("sql");
        System.out.println("same object: " + (probe == tags.get(1)));
        System.out.println("indexOf: " + tags.indexOf(probe));
        System.out.println("other: " + tags.indexOf(new Tag("css")));
    }
}

How It Works

  1. Tags with equals. Two Tag objects are equal when their labels are equal, and the class also overrides hashCode to match.
  2. A different object. probe is a new Tag, so probe == tags.get(1) is false.
  3. indexOf uses equals, not ==. The probe equals the second tag in the list, so the result is 1.
  4. No equal tag. A tag labeled "css" matches nothing, so the answer is -1.

Frequently Asked Questions

The index of the first element equal to the given object, or -1 if the list does not contain it.
Use contains(value), or indexOf(value) >= 0. Both give the same answer.
No, only the first. Use lastIndexOf for the last one, or loop over the list to find every position.
Yes. "Jon" and "jon" are different strings.
It uses equals. Override equals (and hashCode) in your own classes to make indexOf find equal objects.

Next: isEmpty()

Check whether the list has no elements.

isEmpty() 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.

11 people found this page helpful