contains(Object obj) method

contains(Object obj) method: This object obj method uses to check the given element is present in LinkedList or not. It returns only a boolean value. If the given element exists in LinkedList, then it returns true otherwise false.

boolean contains(Object obj);

Where, Object represents the type of class in LinkedList.
obj is the element which you want to check in the LinkedList.
return type:  Its return type is boolean. It can return either true or false.

import java.util.LinkedList;
public class ExampleOfLinkedList 
{
	public static void main(String[] args) 
	{
		LinkedList<String> listOfNames = new LinkedList<String>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("RAVI");
		listOfNames.add("SITE");
		
		// Return, true because RAVI is exist 
		System.out.println("Is 'RAVI' present in LinkedList = 
                "+listOfNames.contains("RAVI"));
		// Return, true because RAVI KANT doesn't exist 
		System.out.println("Is 'RAVI KANT' present in LinkedList = 
                "+listOfNames.contains("RAVI KANT"));
	}
}

Output: Is ‘RAVI’ present in LinkedList = true
Is ‘RAVI KANT’ present in LinkedList = false

Leave a Comment