Tuesday 1 April 2014

retainAll : Retain Elements

boolean retainAll(Collection<?> c)
    Retains only the elements in this list that are contained in the specified collection. Return true if this list changed as a result of the call.


import java.util.*;
class ListRetainAll{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  Set<Integer> mySet1 =new HashSet<> ();
  Set<Integer> mySet2 =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet1 */
  for(int i=5; i < 15; i++){
   mySet1.add(i);
  }
  
  /* Add Elements to mySet2 */
  for(int i=10; i < 15; i++){
   mySet2.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet1 are ");
  System.out.println(mySet1 +"\n");
  System.out.println("Elements in mySet2 are ");
  System.out.println(mySet2 +"\n");
  
  myList.retainAll(mySet1);
  System.out.println("After retaining mySet1 from myList");
  System.out.println(myList +"\n");
  
  myList.retainAll(mySet2);
  System.out.println("After retaining mySet2 from myList");
  System.out.println(myList +"\n");
  
 }
}

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

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

Elements in mySet2 are
[10, 11, 12, 13, 14]

After retaining mySet1 from myList
[5, 6, 7, 8, 9]

After retaining mySet2 from myList
[]

1. throws UnsupportedOperationException if the retainAll operation is not supported by this list

import java.util.*;
class ListRetainAllUnsupported{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> unmodifiable;
  Set<Integer> mySet1 =new HashSet<> ();

  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet1 */
  for(int i=5; i < 15; i++){
   mySet1.add(i);
  }
  
  unmodifiable = Collections.unmodifiableList(myList);
  
  System.out.println("Elements in unmodifiable List are ");
  System.out.println(unmodifiable +"\n");
  System.out.println("Elements in mySet1 are ");
  System.out.println(mySet1 +"\n");
  
  unmodifiable.retainAll(mySet1);
  
 }
}

Since Java don't permit to add any data to unmodifiable lists, Below error thrown while running the program.
Output
Elements in unmodifiable List are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

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

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.retainAll(Unknown Source)
        at ListRetainAllUnsupported.main(ListRetainAllUnsupported.java:26)


2. throws ClassCastException if the class of an element of this list is incompatible with the specified collection

3. throws NullPointerException if this list contains a null element and the specified collection does not permit null elements or if the specified collection is null.











Prevoius                                                 Next                                                 Home

No comments:

Post a Comment