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.

Syntax

Following is the declaration of retainAll() method:

Parameter

ParameterDescriptionRequired/Optional
cIt is a collection of elements which will be retained in this vector, and all other elements are removed.Required

Return

The retainAll() method returns true if the vector changed as a result of the call.

Exceptions

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.

Compatibility Version

Java 1.2 and above

Example 1

Test it Now

Output:

Values in vector :[1, 2, 3, 4, 2]
Values in vector :[2, 3, 2]

Example 2

Test it Now

Output:

Vector: [A, B, C, 10, 20]
Collection retained
Final Vector: [A, B, C]

Next TopicJava Vector




Latest Courses