Collection contains() method in Java with Examples
The contains(Object element) of java.util.Collection interface is used to check whether the element ‘element’ exists in this collection. This method returns a boolean value depicting the presence of the element. If the element is present, it returns true, else it returns false.
Parameters: This method accepts a mandatory parameter element of type Object which is to be checked in this collection.
Return Value: This method returns a boolean value depicting the presence of the element. If the element is added, it returns true, else it returns false.
Exceptions: This method throws following exceptions:
- ClassCastException: if the class of the specified element prevents it from being added to this collection
- NullPointerException: if the specified element is null and this collection does not permit null elements
Below examples illustrate the Collection contains() method:
Example 1: Using LinkedList Class
The list is: [Geeks, for, Geeks] Is Geeks present in the List: true
Example 2: Using ArrayDeque Class
ArrayDeque: [Welcome, To, Geeks, 4, Geeks] Is Geeks present in the ArrayDeque: true
Example 3: Using ArrayList Class
ArrayList: [15, 20, 25] Is 20 present in the ArrayList: true
Example 4: To demonstrate NullPointer Exception
The ArrayList is: 
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.