In Java, lastIndexOf(o) searches the LinkedList from the end toward the front and returns the index of the last element equal to o. If there is no match, it returns -1.
The returned index still counts from the front, as always. When a value appears only once, indexOf and lastIndexOf return the same number.
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.lastIndexOf("B"); // 3, the last B
int b = list.indexOf("B"); // 1, the first B
int c = list.lastIndexOf("Z"); // -1, not found
Call
What it does
list.lastIndexOf(o)
Index of the last match, or -1.
list.indexOf(o)
Index of the first match, or -1.
list.removeLastOccurrence(o)
Removes the last match.
list.contains(o)
Tells only whether a match exists.
Equality is tested with equals, and null is allowed.
Library declarations
Java
// java.util.LinkedList
public int lastIndexOf(Object o)
Parameters
Parameter
Description
o (Object)
The element to look for. May be null.
Return Value
Return Value
Description
int
The index of the last match, or -1 if the list does not contain o.
The two methods differ.indexOf reports 0 and lastIndexOf reports 4.
A single match. The only 4 is at index 3, so the first and last positions would be the same.
A missing value. There is no 9, so the answer is -1.
Example 2 — Measure the Gap Between the First and Last Copy
Use both search methods to see how much of the list lies between the first and last copy of a value.
Java
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListLastIndexOfGap {
public static void main(String[] args) {
LinkedList<String> log = new LinkedList<>(Arrays.asList(
"start", "ok", "ERROR", "ok", "ok", "ERROR", "ok"));
int first = log.indexOf("ERROR");
int last = log.lastIndexOf("ERROR");
System.out.println("first ERROR at " + first);
System.out.println("last ERROR at " + last);
System.out.println("entries between them: " + (last - first - 1));
System.out.println("after the last one: " + (log.size() - 1 - last));
}
}
Output
first ERROR at 2
last ERROR at 5
entries between them: 2
after the last one: 1
How It Works
Two errors. In seven log lines, "ERROR" appears at indexes 2 and 5.
Both ends found.indexOf gives 2 and lastIndexOf gives 5.
Entries in between. Subtracting the two indexes and then 1 gives 2, which are the two "ok" lines between the errors.
Entries after the last one. The last valid index is 6, so one line comes after the final error.
Example 3 — Keep Everything Up to the Last Match
Find the last separator in a list of path parts and use it to split the list in two.
Java
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class LinkedListLastIndexOfCut {
public static void main(String[] args) {
LinkedList<String> parts = new LinkedList<>(
Arrays.asList("home", "/", "user", "/", "notes.txt"));
int cut = parts.lastIndexOf("/");
List<String> folder = new LinkedList<>(parts.subList(0, cut));
List<String> file = new LinkedList<>(parts.subList(cut + 1, parts.size()));
System.out.println("last slash at: " + cut);
System.out.println("folder: " + folder);
System.out.println("file: " + file);
}
}
Output
last slash at: 3
folder: [home, /, user]
file: [notes.txt]
How It Works
Path pieces. The list holds five parts, with a slash at indexes 1 and 3.
lastIndexOf finds the final slash. The result is 3, not 1.
Everything before it.subList(0, cut) copies home, "/" and user into the folder list.
Everything after it.subList(cut + 1, size) gives only the file name, "notes.txt".
Frequently Asked Questions
The index of the last element equal to the given object, or -1 if the list does not contain it.
indexOf returns the first match, and lastIndexOf returns the last match. Both count the index from the front of the list.
When the value appears exactly once, or not at all (both return -1).
Yes. A LinkedList can walk backward from its tail, so a match near the end is found quickly.
Use removeLastOccurrence(o), which finds the last match and deletes it.