Java Program to Get the First Item From LinkedList Collection Using GetFirst()

You are currently viewing Java Program to Get the First Item From LinkedList Collection Using GetFirst()

Java program to get the first item from LinkedList collection using getFirst()

// Java program to get the first item from LinkedList collection 
// using getFirst()

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");

    System.out.println("Country names: " + countries);
    System.out.println("First element: " + countries.getFirst());
  }
}

Output

Country names: [India, USA, UK, CANADA]
First element: India

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