Sunday 1 June 2014

ArrayList : containsAll : Search for a Collection of elements

public boolean containsAll(Collection<?> c)
Returns true if this list contains all of the elements of the specified collection.

import java.util.*;
class ArrayListContainsAll{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  ArrayList<Integer> myList1 = new ArrayList<> ();
  ArrayList<Integer> myList2 = new ArrayList<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to myList1 */
  for(int i=5; i < 10; i++){
   myList1.add(i);
  }
  
  /* Add Elements to myList2 */
  for(int i=5; i < 12; i++){
   myList2.add(i);
  }
 
  
  System.out.println("Elements in the myList are");
  System.out.println(myList+"\n");
  
  System.out.println("Elements in the myList1 are");
  System.out.println(myList1+"\n");
  
  System.out.println("Elements in the myList2 are");
  System.out.println(myList2+"\n");
  
  System.out.print("Is myList contains all the elements of myList1 ");
  System.out.println(myList.containsAll(myList1));
  
  System.out.print("Is myList contains all the elements of myList2 ");
  System.out.println(myList.containsAll(myList2));
 }
}

Output
Elements in the myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in the myList1 are
[5, 6, 7, 8, 9]

Elements in the myList2 are
[5, 6, 7, 8, 9, 10, 11]

Is myList contains all the elements of myList1 true
Is myList contains all the elements of myList2 false


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment