Java Program to Check Whether an Item Exists in the LinkedList Collection or Not

You are currently viewing Java Program to Check Whether an Item Exists in the LinkedList Collection or Not

Java program to check whether an item exists in the LinkedList collection or not

// Java program to check whether an item exists in 
// LinkedList collection or not

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

    if (countries.contains("UK") == true)
      System.out.println("Item exists.");
    else
      System.out.println("Item does not exists.");
  }
}

Output

Country names: [India, USA, UK, CANADA]
Item exists.

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