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: 
- Collection add() method in Java with Examples
- Collection isEmpty() method in Java with Examples
- Collection addAll() method in Java with Examples
- Collection clear() method in Java with Examples
- Stack addAll(int, Collection) method in Java with Example
- Stack addAll(Collection) method in Java with Example
- Iterator vs Collection in Java
- Garbage Collection in Java
- Difference between Arrays and Collection in Java
- Convert an Iterable to Collection in Java
- Output of Java programs | Set 10 (Garbage Collection)
- Sorting collection of String and StringBuffer in Java
- Java Collection| Difference between Synchronized ArrayList and CopyOnWriteArrayList
- How to prevent objects of a class from Garbage Collection in Java
- How to make object eligible for garbage collection in Java?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.