Java Vector retainAll() Method
The retainAll() method of Java Vector class is used to retain only that elements in the vector which are contained in the specified collection. In other words, it removes all elements from this vector that are not contained in the specified collection.
Following is the declaration of retainAll() method:
The retainAll() method returns true if the vector changed as a result of the call.
NullPointerException- This method has thrown an exception if the vector contains one or more null elements and the specified collection does not support null elements or if the specified collection is null.
Java 1.2 and above
Example 1Test it Now
Values in vector :[1, 2, 3, 4, 2] Values in vector :[2, 3, 2]
Example 2Test it Now
Vector: [A, B, C, 10, 20] Collection retained Final Vector: [A, B, C]