The contains() method of Java AbstractSequentialList is used to check whether an element is present in a Collection or not. It takes the element as a parameter and returns True if the element is present in the collection.
Syntax:
public boolean contains(Object element)
Parameters: The parameter element is of type Collection. This parameter refers to the element whose occurrence is needed to be checked in the collection.
Return Value: The method returns a boolean value. It returns True if the element is present in the Collection otherwise it returns False.
Below programs illustrate the AbstractSequentialList.contains() method:
Program 1:
import java.util.*;
public class GFG {
public static void main(String args[])
{
AbstractSequentialList<String>
abs = new LinkedList<String>();
abs.add( "Geeks" );
abs.add( "for" );
abs.add( "Geeks" );
abs.add( "10" );
abs.add( "20" );
System.out.println( "AbstractSequentialList: "
+ abs);
System.out.println( "\nDoes the Collection"
+ " contains 'Hello': "
+ abs.contains( "Hello" ));
System.out.println( "Does the collection"
+ " contains '20': "
+ abs.contains( "20" ));
System.out.println( "Does the Collection"
+ " contains 'Geeks': "
+ abs.contains( "Geeks" ));
}
}
|
Output:
AbstractSequentialList: [Geeks, for, Geeks, 10, 20]
Does the Collection contains 'Hello': false
Does the collection contains '20': true
Does the Collection contains 'Geeks': true
Program 2:
import java.util.*;
public class GFG {
public static void main(String args[])
{
AbstractSequentialList<Integer>
abs = new LinkedList<Integer>();
abs.add( 10 );
abs.add( 20 );
abs.add( 30 );
abs.add( 40 );
abs.add( 50 );
System.out.println( "AbstractSequentialList:"
+ abs);
System.out.println( "\nDoes the Collection "
+ "contains '10': "
+ abs.contains( 10 ));
System.out.println( "\nDoes the Collection"
+ " contains '50': "
+ abs.contains( 50 ));
System.out.println( "Does the collection"
+ " contains '100': "
+ abs.contains( 100 ));
}
}
|
Output:
AbstractSequentialList:[10, 20, 30, 40, 50]
Does the Collection contains '10': true
Does the Collection contains '50': true
Does the collection contains '100': false
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 :
24 Dec, 2018
Like Article
Save Article