Vector retainAll() method in Java with Examples Last Updated : 04 Jan, 2019 Comments Improve Suggest changes Like Article Like Report 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. 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); } } 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. 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); } } 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 Comment More infoAdvertise with us Next Article Vector retainAll() method in Java with Examples S suman_ptnl Follow Improve Article Tags : Java Java - util package Java-Functions Java-Vector Practice Tags : Java Similar Reads Vector indexOf() Method in Java The java.util.vector.indexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the vector does not contain the element. Syntax: 3 min read Vector insertElementAt() Method in Java with Examples insertElementAt() method of Vector class present inside java.util package is used to insert a particular element at the specified index of the Vector. Both the element and the position are passed as the parameters. If an element is inserted at a specified index, then all the elements are pushed upwa 3 min read Vector isEmpty() Method in Java The Java.util.Vector.isEmpty() method in Java is used to check and verify if a Vector is empty or not. It returns True if the Vector is empty else it returns False. Syntax: Vector.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Vectoris 2 min read Vector iterator() method in Java with Examples iterator() method of Vector class that is present inside java.util package is used to return an iterator of the same elements as that of the Vector. The elements are returned in random order from what was present in the vector. Syntax: Iterator iterate_value = Vector.iterator(); Parameters: The func 2 min read Vector lastElement() Method in Java The java.util.vector.lastElement() method in Java is used to retrieve or fetch the last element of the Vector. It returns the element present at the last index of the vector. Syntax: Vector.lastElement() Parameters: The method does not take any parameter. Return Value: The method returns the last el 2 min read Vector lastIndexOf() Method in Java The Java.util.Vector.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present in the vector then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is us 2 min read Vector listIterator() method in Java with Examples java.util.Vector.listIterator() This method returns a list iterator over the elements of a Vector object in proper sequence. It is bidirectional, so both forward and backward traversal is possible, using next() and previous() respectively. The iterator thus returned is fail-fast. This means that str 3 min read Vector removeAll() Method in Java The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified. Syntax: Vector.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed f 2 min read Vector removeAllElements() method in Java with Example The Java.util.Vector.removeAllElements() method is used to removes all components from this Vector and sets its size to zero. Syntax: Vector.removeAllElements() Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Jav 2 min read Vector removeElement() method in Java with Example The java.util.Vector.removeElement() method is used to remove first occurrence of particular object. If object is not found then it returns false else it returns true. If a particular object is present inside vector and removeElement() method call on that vector element then this method reduces vect 3 min read Like