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

// 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"));
  }
}
Country names: [India, USA, UK, CANADA, USA]
Last Index of USA: 4
Last Index of PAK: -1

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

Share on:

Leave a Comment