Friday, 9 May 2014

Vector : retainAll : Retain Collection of Elements

public synchronized boolean retainAll(Collection<?> c)
Retains only the elements in this Vector that are contained in the specified Collection.

import java.util.*;

class VectorRetainAll{
 public static void main(String args[]){
  Vector<Integer> myVector;
  Collection<Integer> myColl;
  
  myVector = new Vector<> ();
  myColl = new HashSet<> ();
  
  /*Add Elements to myVector*/
  myVector.add(10);
  myVector.add(11);
  myVector.add(12);
  myVector.add(13);

  /* Add Elements to myColl */
  myColl.add(12);
  myColl.add(13);
  myColl.add(14);
  
  System.out.println("Elements in myVector ");
  System.out.println(myVector);
  System.out.println("Elements in myColl ");
  System.out.println(myColl);
  
  System.out.println("\nRetaining the elements from myColl");
  myVector.retainAll(myColl);
  System.out.println("\nElements in myVector ");
  System.out.println(myVector);
 }
}

Output
Elements in myVector
[10, 11, 12, 13]
Elements in myColl
[12, 13, 14]

Retaining the elements from myColl

Elements in myVector
[12, 13]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment