Tuesday 27 May 2014

LinkedList : removeAll : Remove collection of elements

boolean removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection (optional operation).

import java.util.*;
class LinkedListRemoveAll{
 public static void main(String args[]){
  LinkedList<Integer> myList = new LinkedList<> ();
  Set<Integer> mySet =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet */
  for(int i=5; i < 15; i++){
   mySet.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  /* Remove mySet Data from myList */
  myList.removeAll(mySet);
  
  System.out.println("Elements in myList after removing mySet are");
  System.out.println(myList); 
 }
}

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

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in myList after removing mySet are
[0, 1, 2, 3, 4]


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment