The containsAll() method of Java AbstractCollection is used to check whether two Collections contain the same elements or not. It takes one collection as a parameter and returns True if all of the elements of this collection is present in the other collection.
Syntax:
AbstractCollection.containsAll(Collection C)
Parameters: The parameter C is a Collection. This parameter refers to the collection whose elements occurrence is needed to be checked in this collection.
Return Value: The method returns True if this collection contains all the elements of other Collection otherwise it returns False.
Below programs illustrate the AbstractCollection.conatinsAll() method:
Program 1:
import java.util.*;
class AbstractCollectionDemo {
public static void main(String args[])
{
AbstractCollection<String>
abs = new LinkedList<String>();
abs.add( "Geeks" );
abs.add( "for" );
abs.add( "Geeks" );
abs.add( "10" );
abs.add( "20" );
AbstractCollection<String>
abs2 = new LinkedList<String>();
abs2.add( "Geeks" );
abs2.add( "for" );
abs2.add( "Geeks" );
abs2.add( "10" );
abs2.add( "20" );
System.out.println( "\nBoth the collections same: "
+ abs.containsAll(abs2));
}
}
|
Output:
Both the collections same: true
Program 2:
import java.util.*;
class AbstractCollectionDemo {
public static void main(String args[])
{
AbstractCollection<String>
abs = new LinkedList<String>();
abs.add( "Geeks" );
abs.add( "for" );
abs.add( "Geeks" );
AbstractCollection<String>
abs2 = new LinkedList<String>();
abs2.add( "10" );
abs2.add( "20" );
System.out.println( "\nBoth the collections same: "
+ abs.containsAll(abs2));
}
}
|
Output:
Both the collections same: 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 :
26 Nov, 2018
Like Article
Save Article