Vector retainAll() method in Java with Examples
Last Updated :
04 Jan, 2019
The retainAll method of class vector in java is an inbuilt function in Java which is used to retains only the elements in this Vector that are contained in the specified Collection. In other words, removes from this Vector all of its elements that are not contained in the specified Collection.
Syntax:
public boolean retainAll(Collection c)
Parameter: Here c is a collection of elements to be retained in this Vector (all other elements are removed).
Return value: This method will return a boolean value, i.e true if this Vector changed as a result of the call or else false.
Exception: This method will throw the following exceptions.
ClassCastException – If the types of one or more elements in this vector are incompatible with the specified collection.
NullPointerException –If this vector contains one or more null elements and the specified collection does not support null elements, or if the specified collection is null.
Below programs illustrate the Vector.retainAll() method in Java:
Program 1:To illustrate Vector.retainAll() method in Java.
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String args[])
{
Vector<String> v1 = new Vector<String>();
v1.add( "Geeks" );
v1.add( "For" );
v1.add( "Geeks" );
v1.add( "is" );
v1.add( "a" );
v1.add( "computer" );
v1.add( "science" );
v1.add( "portal" );
System.out.println( "Elements of vector1:" + v1);
Vector<String> v2 = new Vector<String>();
v2.add( "Geeks" );
v2.add( "For" );
v2.add( "Geeks" );
v2.add( "contains" );
v2.add( "well" );
v2.add( "written" );
v2.add( "programming" );
v2.add( "articles" );
v2.add( "and" );
v2.add( "much" );
v2.add( "more." );
System.out.println( "Elements of vector2:" + v2);
System.out.println( "Calling retainAll() method" );
v1.retainAll(v2);
System.out.println( "After calling retainAll() method" );
System.out.println(v1);
}
}
|
Output:
Elements of vector1:[Geeks, For, Geeks, is, a, computer, science, portal]
Elements of vector2:[Geeks, For, Geeks, contains, well, written, programming, articles, and, much, more.]
Calling retainAll() method
After calling retainAll() method
[Geeks, For, Geeks]
Program 2:To show return value of retainAll() method in Java.
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String args[])
{
Vector<String> v1 = new Vector<String>();
v1.add( "Geeks" );
v1.add( "For" );
v1.add( "Geeks" );
v1.add( "is" );
v1.add( "a" );
v1.add( "computer" );
v1.add( "science" );
v1.add( "portal" );
System.out.println( "Elements of vector1:" + v1);
Vector<String> v2 = new Vector<String>();
v2.add( "Geeks" );
v2.add( "For" );
v2.add( "Geeks" );
v2.add( "contains" );
v2.add( "well" );
v2.add( "written" );
v2.add( "programming" );
v2.add( "articles" );
v2.add( "and" );
v2.add( "many" );
v2.add( "more." );
System.out.println( "Elements of vector1:" + v1);
boolean t = v1.retainAll(v2);
System.out.println( "Calling retainAll() method: " );
System.out.println(t);
}
}
|
Output:
Elements of vector1:[Geeks, For, Geeks, is, a, computer, science, portal]
Elements of vector1:[Geeks, For, Geeks, is, a, computer, science, portal]
Calling retainAll() method:
true
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...