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.
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.
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.
Please Login to comment...