Friday 18 April 2014

HashSet : retainAll : Retain all the Elements from a Collection

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

import java.util.*;
class HashSetRetainAll{
 public static void main(String args[]){
  Set<Integer> mySet1 = new HashSet<Integer> ();
  Set<Integer> mySet2 = new HashSet<Integer> ();
  Set<Integer> mySet3 = new HashSet<Integer> ();
  
  List<Integer> myList1 = new ArrayList<Integer> ();
  List<Integer> myList2 = new ArrayList<Integer> ();
  List<Integer> myList3 = new ArrayList<Integer> ();
  
  /*Adding Elements to the mySet1 */
  for(int i=0; i < 10; i++)
   mySet1.add(i);
   
  /* Copy data from mySet1 to mySet2 */
  mySet2.addAll(mySet1);
  mySet3.addAll(mySet1);
  myList3.addAll(mySet1);
  
  /* Add Elements to myList1 */
  for(int i=0; i < 20; i+=2)
   myList1.add(i);
   
  /* Add Elements to myList2 */
  for(int i=15; i< 25; i++)
   myList2.add(i);
   
  /* Display the Data */
  System.out.println("Data in mySet1 is \n" + mySet1); 
  System.out.println("\nData in mySet2 is \n" + mySet2);
  System.out.println("\nData in mySet3 is \n" + mySet3);
  System.out.println("\nData in myList1 is \n" + myList1);
  System.out.println("\nData in myList2 is \n" + myList2);
  System.out.println("\nData in myList3 is \n" + myList3);
  
  /* Retain the data of myList1 from mySet1 */
  System.out.print("\nIs mySet1 changed after retaining myList1 ");
  System.out.println(mySet1.retainAll(myList1));
  
  /* Retain the data of myList2 from mySet2 */
  System.out.print("\nIs mySet2 changed after retaining myList2 ");
  System.out.println(mySet2.retainAll(myList2));
  
  /* Retain the data of myList3 from mySet3 */
  System.out.print("\nIs mySet3 changed after retaining myList3 ");
  System.out.println(mySet3.retainAll(myList3));
  
  System.out.println("\nElements in mySet1 After retaining myList1 is");
  System.out.println(mySet1);
  System.out.println("\nElements in mySet2 After retaining myList2 is");
  System.out.println(mySet2);
  System.out.println("\nElements in mySet3 After retaining myList3 is");
  System.out.println(mySet3);
 }
}


Output
Data in mySet1 is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Data in mySet2 is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Data in mySet3 is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Data in myList1 is
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Data in myList2 is
[15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

Data in myList3 is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Is mySet1 changed after retaining myList1 true

Is mySet2 changed after retaining myList2 true

Is mySet3 changed after retaining myList3 false

Elements in mySet1 After retaining myList1 is
[0, 2, 4, 6, 8]

Elements in mySet2 After retaining myList2 is
[]

Elements in mySet3 After retaining myList3 is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


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

Example
If program tries to perform retainAll on un modifiable Set, then Jave run time throws UnsupportedOperationException.


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

  /*Adding Elements to the mySet1 */
  for(int i=0; i < 10; i++)
   mySet1.add(i);
 
  /* Add Elements to myList1 */
  for(int i=0; i < 20; i+=2)
   myList1.add(i);
   
  unmodifySet = Collections.unmodifiableSet(mySet1);
   
  /* Trying to Perform retainAll on unmodifySet */
  unmodifySet.retainAll(myList1);
 }
}


Output

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.retainAll(Collections.ja
va:1091)
        at HashSetRetainAllUnsupported.main(HashSetRetainAllUnsupported.java:19)








Prevoius                                                 Next                                                 Home

No comments:

Post a Comment