Open In App

Collection contains() method in Java with Examples

Last Updated : 29 Nov, 2018
Comments
Improve
Suggest changes
3 Likes
Like
Report
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 Java
// Java code to illustrate boolean contains() method import java.io.*; import java.util.*; public class GFG {  public static void main(String args[])  {  // creating an empty LinkedList  Collection<String> list = new LinkedList<String>();  // use add() method to add elements in the list  list.add("Geeks");  list.add("for");  list.add("Geeks");  // Output the present list  System.out.println("The list is: " + list);  // Checking the presence of Geeks  // using contains() method  boolean result = list.contains("Geeks");  // printing the result  System.out.println("Is Geeks present in the List: "  + result);  } } 
Output:
 The list is: [Geeks, for, Geeks] Is Geeks present in the List: true 
Example 2: Using ArrayDeque Class Java
// Java code to illustrate contains() method import java.util.*; public class ArrayDequeDemo {  public static void main(String args[])  {  // Creating an empty ArrayDeque  Collection<String> de_que = new ArrayDeque<String>();  // Use add() method to add elements into the Deque  de_que.add("Welcome");  de_que.add("To");  de_que.add("Geeks");  de_que.add("4");  de_que.add("Geeks");  // Displaying the ArrayDeque  System.out.println("ArrayDeque: " + de_que);  // Checking the presence of Geeks  // using contains() method  boolean result = de_que.contains("Geeks");  // printing the result  System.out.println("Is Geeks present in the ArrayDeque: "  + result);  } } 
Output:
 ArrayDeque: [Welcome, To, Geeks, 4, Geeks] Is Geeks present in the ArrayDeque: true 
Example 3: Using ArrayList Class Java
// Java code to illustrate contains() method import java.io.*; import java.util.*; public class ArrayListDemo {  public static void main(String[] args)  {  // create an empty array list with an initial capacity  Collection<Integer> arrlist = new ArrayList<Integer>(5);  // use add() method to add elements in the list  arrlist.add(15);  arrlist.add(20);  arrlist.add(25);  // Output the present list  System.out.println("ArrayList: " + arrlist);  // Checking the presence of 20  // using contains() method  boolean result = arrlist.contains(20);  // printing the result  System.out.println("Is 20 present in the ArrayList: "  + result);  } } 
Output:
 ArrayList: [15, 20, 25] Is 20 present in the ArrayList: true 
Example 4: To demonstrate NullPointer Exception Java
// Java code to illustrate boolean contains() import java.util.*; public class LinkedListDemo {  public static void main(String args[])  {  // Creating an empty ArrayList  Collection<String>  list = new ArrayList<String>();  // Displaying the list  System.out.println("The ArrayList is: " + list);  try {  // Checking presence of null  list.contains(null);  }  catch (Exception e) {  System.out.println("Exception: " + e);  }  } } 
Output:
 The ArrayList is: [] 
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html#contains-java.lang.Object-

Explore