beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – Check if a particular element exists in LinkedList example

By Chaitanya Singh | Filed Under: Java Collections

In this example we are gonna see how to check if a particular element exists in LinkedList using contains() method:

public boolean contains(Object o): Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

import java.util.LinkedList;

public class CheckLinkedList {
 public static void main(String[] args) {

    // Creating LinkedList of String Elements
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Populating it with String values
    linkedlist.add("AA");
    linkedlist.add("BB");
    linkedlist.add("CC");
    linkedlist.add("DD");
    linkedlist.add("EE");

    // contains() method checks whether the element exists
    if (linkedlist.contains("CC")) {
       System.out.println("Element CC is present in List");
    } else {
       System.out.println("List doesn't have element CC");
     }
    
    //Checking for element FF
    if (linkedlist.contains("FF")) {
       System.out.println("Element FF is present in List");
    } else {
        System.out.println("List doesn't have element FF");
      }
 }
}

Output:

Element CC is present in List
List doesn't have element FF

Enjoyed this post? Try these related posts

  1. Java – Get size of HashMap example
  2. How to get sub list of Vector example in java
  3. Replace Vector elements using index – Java example
  4. TreeMap Iterator example – Java
  5. How to sort ArrayList in Java
  6. Java ArrayList indexOf() Method example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap