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.
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[]) { // Creating an empty Vector Vector<String> v1 = new Vector<String>(); // adding elements to the vector v1 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); // Creating an other empty vector Vector<String> v2 = new Vector<String>(); // adding elements to the vector v2 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"); // calling retainAll() v1.retainAll(v2); System.out.println("After calling retainAll() method"); System.out.println(v1); } } |
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[]) { // Creating an empty Vector Vector<String> v1 = new Vector<String>(); // adding elements to the vector v1 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); // Creating an other empty vector Vector<String> v2 = new Vector<String>(); // adding elements to the vector v2 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); // calling retainAll() boolean t = v1.retainAll(v2); System.out.println("Calling retainAll() method: "); System.out.println(t); } } |
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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- AbstractCollection retainAll() method in Java with Examples
- CopyOnWriteArrayList retainAll() method in Java with Examples
- List retainAll() Method in Java with Examples
- LinkedTransferQueue retainAll() method in Java with Examples
- LinkedBlockingDeque retainAll() method in Java with Examples
- SortedSet retainAll() method in Java with Examples
- ArrayList retainAll() method in Java
- LinkedHashSet retainAll() method in Java with Example
- AbstractSet retainAll() method in Java with Example
- Stack retainAll() method in Java with Example
- HashSet retainAll() method in Java with Example
- AbstractSequentialList retainAll() method in Java with Example
- CopyOnWriteArraySet retainAll() method in Java with Example
- TreeSet retainAll() method in Java with Example
- Set retainAll() method in Java with Example
- Vector clone() Method in Java with Examples
- Vector toArray() Method in Java with Examples
- Vector iterator() method in Java with Examples
- Vector listIterator() method in Java with Examples
- Vector clear() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

