Home Java LinkedList Methods indexOf() Java LinkedList indexOf() Method 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.
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 Call What 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.util.LinkedList
public int indexOf(Object o) Parameters Parameter Description o (Object)The element to look for. May be null.
Return Value Return Value Description intThe index of the first match, or -1 if the list does not contain o.
Important
The search is linear: Java checks the elements one by one, so a long list takes longer. The result -1 is not an error, but using it as an index in get(-1) throws IndexOutOfBoundsException, so always test for it first. The comparison is case-sensitive for strings.
Compare with Similar Methods Ways to search a list:
Java Examples 1. Find a position 2. Test before using 3. Uses equals 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.
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"));
}
} Kim: 2
Jon: 1
Zed: -1
jon: -1 How It Works Kim is unique. The search finds her at index 2.Jon appears twice. indexOf stops at the first match, index 1, and ignores the one at index 3.No match gives -1. "Zed" is not in the list.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.
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));
}
} task-3 is at 2, ahead of it: 2
task-9 is not in the queue
null index: -1 How It Works A queue of tasks. The three tasks sit at indexes 0, 1 and 2.Test the result. An index of 0 or more means the task was found, and -1 means it was not.task-3 is found. It is at index 2, which also means that two tasks are ahead of it.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.
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")));
}
} same object: false
indexOf: 1
other: -1 How It Works Tags with equals. Two Tag objects are equal when their labels are equal, and the class also overrides hashCode to match.A different object. probe is a new Tag, so probe == tags.get(1) is false.indexOf uses equals, not ==. The probe equals the second tag in the list, so the result is 1.No equal tag. A tag labeled "css" matches nothing, so the answer is -1.Frequently Asked Questions What does LinkedList indexOf() return? The index of the first element equal to the given object, or -1 if the list does not contain it.
How do I check if a LinkedList contains a value? Use contains(value), or indexOf(value) >= 0. Both give the same answer.
Does indexOf() find all matches? No, only the first. Use lastIndexOf for the last one, or loop over the list to find every position.
Is indexOf() case-sensitive for strings? Yes. "Jon" and "jon" are different strings.
How does indexOf() compare objects? It uses equals. Override equals (and hashCode) in your own classes to make indexOf find equal objects.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion