How to Check If a Specified Element in LinkedList in Java


In this example, we will learn how to check if a specified element in the LinkedList in Java.

Source Code

package com.beginner.examples;

import java.util.LinkedList;

public class ElementInLinkedList {

  public static void main(String[] args) {
    LinkedList lists = new LinkedList();
    lists.add("1");
    lists.add("2");

    boolean b = lists.contains("2"); // check if a specified element in the LinkedList
    System.out.println(b);
  }
}

Output:

true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments