Vector class retainAll(Collection> c) method example
Retains only the elements in this Vector that are contained in the specified Collection.
Vector class retainAll(Collection c) method example. This example shows you how to use retainAll(Collection c) method.This method Retains only the elements in this Vector that are contained in the specified Collection.
Here is the code:-
/*
* @Program that Retains only the elements in this Vector that are contained in
the specified Collection.
* RetainAll.java
* Author:-RoseIndia Team
* Date:-26-May-2008
*/
import java.util.*;
public class RetainAll {
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.add("Girish");
l.add("Tewari");
Vector v = new Vector();
v.add("Rose");
v.add("India");
v.add("Rohini");
v.add("Girish");
v.add("Tewari");
Enumeration e = v.elements();
System.out.print("Elements of the vector before retaining is : ");
while (e.hasMoreElements()) {
System.out.print(e.nextElement() + "\t");
}
//Retains only the elements in this Vector that are contained in the specified
//Collection.
boolean b = v.retainAll(l);
System.out.println();
System.out.print("Elements of the vector are retained : " + b);
Enumeration e1 = v.elements();
System.out.println();
System.out.print("Elements of the vector after retaining is : ");
while (e1.hasMoreElements()) {
System.out.print(e1.nextElement() + "\t");
}
}
}
|
Output of the program:-
Elements of the vector before retaining is : Rose India Rohini Girish Tewari
Elements of the vector are retained : true
Elements of the vector after retaining is : Girish Tewari |
|