Java LinkedList lastIndexOf() Method with Examples

In the previous article, we have discussed about Java LinkedList IndexOf() Method with Examples

In this article we are going to see the use of Java LinkedList lastIndexOf() method along with suitable examples.

Java LinkedList lastIndexOf() Method with Examples

This java.util.LinkedList.lastIndexOf() method is used to get the last index position of the specified element in the LinkedList. It returns the last index position of the specified element (if exists) else returns -1.

Syntax:

LinkedListlastName.IndexOf()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see different examples to understand it more clearly.

Exmple-1: Java LinkedList lastIndexOf() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then find the last index of the specified element using lastIndexOf() method.
  • Print the index position.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("book");
        l.add("copy");
        l.add("pen");
        l.add("book");
        l.add("pen");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
       // get the position of specified element occurred last
       System.out.println("Last Occurrence of book: " + l.lastIndexOf("book"));
       System.out.println("Last Occurrence of notebook: " + l.lastIndexOf("notebook"));
    }
}
Output:

The elements of LinkedList are: [book, copy, pen, book, pen]
Last Occurrence of book: 3
Last Occurrence of notebook: -1

Example-2: Java LinkedList lastIndexOf() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then find the last index of the specified element using lastIndexOf() method.
  • Print the index position.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
       // get the position of specified element occurred last
        System.out.println("Last Occurrence of 2: " + l.lastIndexOf(2));
        System.out.println("Last Occurrence of 99: " + l.lastIndexOf(99));
    }
}
Output:

The elements of LinkedList are: [2, 52, 13, 17, 1, 2, 52, 13, 17]
Last Occurrence of 2: 5
Last Occurrence of 99: -1

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: