Java LinkedList - contains() Method
The java.util.LinkedList.contains() method is used to check whether the list contains the specified element or not. It returns true if the list contains the specified element, else returns false.
Syntax
public boolean contains(Object obj)
Parameters
obj |
Specify element whose presence in the list need to be tested. |
Return Value
Returns true if the list contains the specified element, else returns false.
Exception
NA
Example:
In the example below, the java.util.LinkedList.contains() method is used to check the presence of specified element in the given list.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a linkedlist LinkedList<Integer> MyList = new LinkedList<Integer>(); //populating linkedlist MyList.add(10); MyList.add(20); MyList.add(30); //checking the presence of elements for(int i = 5; i <= 20; i += 5) { if(MyList.contains(i)) System.out.println(i +" is present in MyList."); else System.out.println(i +" is NOT present in MyList."); } } }
The output of the above code will be:
5 is NOT present in MyList. 10 is present in MyList. 15 is NOT present in MyList. 20 is present in MyList.
❮ Java.util - LinkedList