In this section, we will learn what the LinkedList contains() method is and how to use it in Java.
What is Java LinkedList contains() Method?
The Java LinkedList contains() method is used to check and see if a LinkedList object has a specific value in its body or not.
Java contains() Method Syntax:
public boolean contains(Object o)
contains() Method Parameters:
The method takes one argument and that is the value we want to search the target LinkedList object for to see if it has this element or not.
contains() Method Return Value:
The return value of this method is of type boolean.
If the target LinkedList object had this element, the return value will be true, otherwise the value false will return instead.
contains() Method Exceptions:
The method does not throw an exception.
Example: using LinkedList contains() method
import java.util.LinkedList; class Main{ public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.addFirst("John"); list.addFirst("Omid"); list.addFirst("Jack"); list.addFirst("Ellen"); list.addFirst("Elon"); System.out.println(list.contains("Omid")); System.out.println(list.contains("Monica")); } }
Output:
true false