The contains() method of List interface in Java is used for checking if the specified element exists in the given list or not.
public boolean contains(Object obj) object-element to be searched for
Parameters: This method accepts a single parameter obj whose presence in this list is to be tested.
Return Value: It returns true if the specified element is found in the list else it returns false.
Below programs illustrate the contains() method in List:
Program 1: Demonstrate the working of the method contains() in List of integer.
The list contains 2 The list does not contains 5
Program 2: Demonstrate the working of the method contains() in List of string.
The list contains geeks The list does not contains coding
Practical Application: In search operations, we can check if a given element exists in a list or not.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.