containsAll(Collection c) method of List Interface in Java

containsAll(Collection c) method of List Interface in Java


Posted in : Core Java Posted on : January 4, 2011 at 7:41 PM Comments : [ 0 ]

In the following example we will show you the method of checking a list from the existing/specified list using containsAll(Collection c) method of List Interface in java.

containsAll(Collection c) method of List Interface in Java

In the following example we will show you the method of checking a list from the existing/specified list using containsAll(Collection c) method of List Interface in java.

Syntax

boolean public containsAll(Collection c)

This method returns either True or False.

In this method you can check a collection from the existing list. It checks a collection from the specified collection and returns true if a collection is found or returns false if a collection is not found. When all the elements of a specified list is found in the existing list then it returns 'true' otherwise 'false'. containsAll(Collection c) means that calling of a.containsAll(b) will return true, if and only if, for each element of 'b' in 'a', would return true. This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it's contained in this collection. If all elements are so contained, 'true' is returned, otherwise false. There is no matter the elements of list 'a' and 'b' are in which order.

Parameter Description

Collection c : It takes an argument as collection (list)

Example of containsAll(Collection c) method

In this example we will show you how does a containsAll(Collection c) method work in List Interface. This example will help you to understand how all elements of a list can be checked into the specified list. Through this example we will show you adding elements to the list, count the number of list, and checking all the elements of a specified list from the existing list.

Example:-

package Devmanuals.com;
import java.util.List;
import java.util.ArrayList;
public class ListContainsAll {
public static void main (String args []){
List l1= new ArrayList();
l1.add("Ram");
l1.add("a");
l1.add("is");
l1.add("boy");
System.out.println("The Element of list1 is : "+l1);
System.out.println("The size of list1 is : "+l1.size());
List l2=new ArrayList();
l2.add("Ram");
l2.add("boy");
l2.add("is");
l2.add("a");
System.out.println("The Element of list2 is : "+l2);
System.out.println("The size of list2 is : "+l2.size());
l1.containsAll(l2);
System.out.println("The Elements are found : "+l1.containsAll(l2));
}
}

Output :

The Element of list1 is : [Ram, a, is, boy]

The size of list1 is : 4

The Element of list2 is : [Ram, boy, is, a]

The size of list2 is : 4

The Elements are found : true

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics