Java ArrayList containsAll() Method
What ia an ArrayList?
Arraylist is a part of collection framework in java. It contains some inbuilt function like replaceAll() function which can be found in the java.util.ArrayList Package.It implements the List interface of the collections framework.The arrayList class have many inbuilt methods, one of those method is containsAll() method
On this page we will discuss about theArrayList containsAll() Method
Java ArrayList containsAll() Method
The ArrayList class in java have several inbuilt methods which can be used to perform different operations. One of the method is ArrayList contailsAll() method ,which is used to check if the arraylist contains all the element of the other list/collection or not.
Syntax :
arraylist.containsAll(collection c);
Parameters:
This methods takes a single parameter collection, whose elements are needed to be checked if they are present in the list or not.
Return Value:
True : If all the elements of the collection are present in the list. False : If all the elements of the collection are not present in the list.
Exceptions:
This method throws a null pointer exception if the collection is empty.
ArrayList containsAll() Method
Example :
import java.util.*; public class Main { public static void main (String args[]) { // Creating an empty Arraylist List < String > list = new ArrayList < String > (); // Use add() method to add elements // into the List list.add ("Welcome"); list.add ("To"); list.add ("Prep"); list.add ("Insta"); list.add ("Class"); // Display the list System.out.println ("List: " + list); // Creating another empty List List < String > listTemp = new ArrayList < String > (); // add elements to listTemp listTemp.add ("Prep"); listTemp.add ("Class"); listTemp.add ("Insta"); // Creating another empty list List < String > listTemp2 = new ArrayList < String > (); // add elements to listTemp2 listTemp2.add ("Prep"); listTemp2.add ("Java"); listTemp2.add ("Insta"); System.out.println ("list contains all the elements of listTemp :" + list.containsAll (listTemp)); System.out.println ("list contains all the elements of listTemp2 :" + list.containsAll (listTemp2)); } }
Output :
List: [Welcome, To, Prep, Insta, Class] list contains all the elements of listTemp :true list contains all the elements of listTemp2 :false
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment