Java Stack lastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Inherited from Vector

Definition

In Java, lastIndexOf(o) scans a Stack from the top down and returns the index of the first element equal to o that it meets: the copy closest to the top. The index itself is still counted from the bottom, starting at 0. No match gives -1.

Stack's own search() is built on this method: search(o) equals size() - lastIndexOf(o) whenever the element exists. The overload lastIndexOf(o, index) starts the downward scan at a given index.

An instance method inherited from java.util.Vector (since Java 1.0). Stack does not override it: index 0 is the bottom of the stack and index size() - 1 is the top.

Syntax

Pass the value to find, and optionally the index where the downward scan starts.

Java
Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.push("a");
stack.push("c");                         // [a, b, a, c]
int last = stack.lastIndexOf("a");       // 2
int first = stack.indexOf("a");          // 0
int below = stack.lastIndexOf("a", 1);   // 0  (scan from index 1 down)
int none = stack.lastIndexOf("z");       // -1
CallWhat it does
stack.lastIndexOf(o)Index of the match closest to the top, or -1.
stack.lastIndexOf(o, i)The same, scanning down from index i.
stack.size() - stack.lastIndexOf(o)The value search(o) returns when o is present.
stack.indexOf(o)The match closest to the bottom instead.

Like indexOf, the scan is linear, but it finishes quickly when the match is near the top.

Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized int lastIndexOf(Object o)
public synchronized int lastIndexOf(Object o, int index)

Parameters

ParameterDescription
o (Object)The value to find, compared with equals(); null is allowed.
index (int)Only in the two-argument form: where the downward scan starts.

Return Value

Return ValueDescription
intThe 0-based index (from the bottom) of the match closest to the top, or -1.

Compare with Similar Methods

Related lookups:

MethodHow it differs
indexOf(o)Finds the match closest to the bottom.
search(o)Turns this result into a 1-based distance from the top.
lastElement()Returns the top element itself, not a position.

Java Examples

Example 1 — Find the Most Recent Repeated Edit

An undo log contains the same kind of edit twice; compare the newest and the oldest match.

Java
import java.util.Stack;

public class StackLastIndexOfDemo {
    public static void main(String[] args) {
        Stack<String> edits = new Stack<>();
        edits.push("type");
        edits.push("paste");
        edits.push("type");
        edits.push("bold");

        System.out.println("edits: " + edits);
        System.out.println("lastIndexOf(type): " + edits.lastIndexOf("type"));
        System.out.println("indexOf(type):     " + edits.indexOf("type"));
        System.out.println("lastIndexOf(cut):  " + edits.lastIndexOf("cut"));
    }
}

How It Works

  1. Scan from the top. lastIndexOf starts at bold (index 3), moves down, and stops at the newer "type" at index 2.
  2. indexOf goes the other way. Scanning up from the bottom, indexOf meets the older "type" first, at index 0.
  3. Absent means -1. No cut edit was recorded, so the result is -1.

Example 3 — Walk Back Through Matches with lastIndexOf(o, from)

List every visit to the home page, newest first.

Java
import java.util.Stack;

public class StackLastIndexOfFrom {
    public static void main(String[] args) {
        Stack<String> visits = new Stack<>();
        visits.push("home");
        visits.push("shop");
        visits.push("home");
        visits.push("blog");
        visits.push("home");

        int i = visits.lastIndexOf("home");
        while (i != -1) {
            System.out.println("home at index " + i);
            i = visits.lastIndexOf("home", i - 1);
        }
    }
}

How It Works

  1. Newest visit first. The one-argument call returns 4, the home visit at the top.
  2. Keep scanning down. Each lastIndexOf("home", i - 1) resumes just below the previous match, finding 2 and then 0.
  3. A negative start ends the loop. After index 0 the next start is -1; the method returns -1 for that instead of throwing, which stops the while loop.

Frequently Asked Questions

The index of the matching element closest to the top, counted from the bottom starting at 0, or -1 if there is no match.
search(o) returns size() - lastIndexOf(o) when o is present and -1 otherwise. Both pick the copy nearest the top.
indexOf() returns the match nearest the bottom; lastIndexOf() returns the match nearest the top. Without duplicates both give the same index.
The one-argument version cannot. lastIndexOf(o, index) throws IndexOutOfBoundsException if index is greater than or equal to size().
equals(), so equal values match even if they are different objects. A null argument is matched with ==.

Next: remove()

Remove by index (returns the element) or by value (returns boolean).

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

14 people found this page helpful