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
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String args[])
{
Collection<String> list = new LinkedList<String>();
list.add( "Geeks" );
list.add( "for" );
list.add( "Geeks" );
System.out.println( "The list is: " + list);
boolean result = list.contains( "Geeks" );
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
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
Collection<String> de_que = new ArrayDeque<String>();
de_que.add( "Welcome" );
de_que.add( "To" );
de_que.add( "Geeks" );
de_que.add( "4" );
de_que.add( "Geeks" );
System.out.println( "ArrayDeque: " + de_que);
boolean result = de_que.contains( "Geeks" );
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
import java.io.*;
import java.util.*;
public class ArrayListDemo {
public static void main(String[] args)
{
Collection<Integer> arrlist = new ArrayList<Integer>( 5 );
arrlist.add( 15 );
arrlist.add( 20 );
arrlist.add( 25 );
System.out.println( "ArrayList: " + arrlist);
boolean result = arrlist.contains( 20 );
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
import java.util.*;
public class LinkedListDemo {
public static void main(String args[])
{
Collection<String>
list = new ArrayList<String>();
System.out.println( "The ArrayList is: " + list);
try {
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-
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Nov, 2018
Like Article
Save Article