Java Program to Get the Index of the Last Occurrence of the Item From LinkedList Collection

You are currently viewing Java Program to Get the Index of the Last Occurrence of the Item From LinkedList Collection

Java program to get the index of the last occurrence of the item from LinkedList collection

// Java program to get the index of the last occurrence of the 
// item from LinkedList collection

import java.util.LinkedList;

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

    countries.add("India");
    countries.add("USA");
    countries.add("UK");
    countries.add("CANADA");
    countries.add("USA");

    System.out.println("Country names: " + countries);
    System.out.println("Last Index of USA: " + countries.lastIndexOf("USA"));
    System.out.println("Last Index of PAK: " + countries.lastIndexOf("PAK"));
  }
}

Output

Country names: [India, USA, UK, CANADA, USA]
Last Index of USA: 4
Last Index of PAK: -1

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply