How to Check If LinkedList Contains Specified Element in Java


This example demonstrates how to check if a particular element exists in Java LinkedList object with contains method.

Source Code

package com.beginner.examples;

import java.util.LinkedList;

public class CheckElementOfLinkedListExample {

	public static void main(String[] args) {

		LinkedList ll = new LinkedList();

		ll.add("1");
		ll.add("3");
		ll.add("5");
		ll.add("7");

		boolean b1 = ll.contains("5");
		boolean b2 = ll.contains("2");

		System.out.println("Element 5---->" + b1 + "nElement 2---->" + b2);

	}

}

Output:

Element 5---->true
Element 2---->false

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments