Java Reference Library →

LinkedList lastIndexOf() method - Syntax and Examples

LinkedList lastIndexOf() method - Syntax and Examples

Java LinkedList lastIndexOf() method - Syntax and Examples

This reference explains LinkedList lastIndexOf() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

LinkedList lastIndexOf() method - Syntax and Examples

Introduction

The lastIndexOf() method in Java's LinkedList class helps you find the last occurrence of a specific object within your linked list. Think of it like searching for something on a shelf – instead of finding the *first* one, this method finds the *last* one.

Syntax

public int lastIndexOf(Object o)

Parameters

Parameter Description
o The object to search for in the linked list. Can be null.

Return Value

The method returns the index of the last occurrence of the specified element in this list, or -1 if it is not found.

Examples

Example 1: Finding a String

Let's start with a simple example. We'll create a linked list of strings and use lastIndexOf() to find the last occurrence of a particular string.

import java.util.LinkedList;

public class LinkedListLastIndexOfExample1 {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("apple");
        list.add("banana");
        list.add("orange");
        list.add("banana");
        list.add("grape");

        int index = list.lastIndexOf("banana");
        System.out.println("Last index of 'banana': " + index);
    }
}
Last index of 'banana': 3

In this example, the linked list contains “banana” twice. The method finds the last occurrence at index 3.

Example 2: Finding a Null Value

Now let’s see how to find the last occurrence of a null value in our LinkedList.

import java.util.LinkedList;

public class LinkedListLastIndexOfExample2 {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("apple");
        list.add(null);
        list.add("orange");
        list.add(null);
        list.add("grape");

        int index = list.lastIndexOf(null);
        System.out.println("Last index of null: " + index);
    }
}
Last index of null: 3

Here, we added two 'null' values to the list. The `lastIndexOf(null)` method correctly identifies that the last occurrence is at index 3.

Example 3: Element Not Found

What happens if the element you are searching for doesn’t exist in the linked list? Let's find out.

import java.util.LinkedList;

public class LinkedListLastIndexOfExample3 {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("apple");
        list.add("banana");
        list.add("orange");

        int index = list.lastIndexOf("kiwi");
        System.out.println("Last index of 'kiwi': " + index);
    }
}
Last index of 'kiwi': -1

Since “kiwi” isn’t in the list, lastIndexOf() returns -1.

Example 4: Searching an Empty List

Now consider searching for an element within an empty linked list.

import java.util.LinkedList;

public class LinkedListLastIndexOfExample4 {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();

        int index = list.lastIndexOf("apple");
        System.out.println("Last index of 'apple': " + index);
    }
}
Last index of 'apple': -1

As expected, searching for any element in an empty linked list will always return -1.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →