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

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

// Java program to get the index of the first 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("Index of USA: " + countries.indexOf("USA"));
    System.out.println("Index of PAK: " + countries.indexOf("PAK"));
  }
}

Output

Country names: [India, USA, UK, CANADA, USA]
Index of USA: 1
Index of PAK: -1
Share on:

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

Leave a Comment