VOOZH about

URL: https://www.geeksforgeeks.org/java/collection-contains-method-in-java-with-examples/

⇱ Collection contains() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collection contains() method in Java with Examples

Last Updated : 29 Nov, 2018
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. Syntax:
Collection.contains(Object element)
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
Output:
The list is: [Geeks, for, Geeks]
Is Geeks present in the List: true
Example 2: Using ArrayDeque Class
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
Is Geeks present in the ArrayDeque: true
Example 3: Using ArrayList Class
Output:
ArrayList: [15, 20, 25]
Is 20 present in the ArrayList: true
Example 4: To demonstrate NullPointer Exception
Comment