Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java 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.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M