Vector removeIf() method in Java Last Updated : 18 Sep, 2018 Comments Improve Suggest changes Like Article Like Report The removeIf() method of Vector removes all of those elements from Vector which satisfies the condition passed as a parameter to this method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition checking function, which checks the given input for a given condition and returns a boolean result for the same indicating whether the condition was met or not. Syntax: public boolean removeIf(Predicate<? super E> filter) Parameter: This method takes a parameter filter which represents a predicate which returns true for elements to be removed. Returns: This method returns True if predicate returns true and some elements were removed. Exception: This method throws NullPointerException if the specified filter is null. Below programs illustrate removeIf() method of Vector: Example 1: To demonstrate removeIf() method on vector which contains a set of Numbers and only the numbers which are divisible by 2 will be removed. Java // Java Program Demonstrate removeIf() // method of Vector import java.util.*; public class GFG { public static void main(String[] args) { // create an Vector which going to // contains a list of Numbers Vector<Integer> Numbers = new Vector<Integer>(); // Add Number to list Numbers.add(22); Numbers.add(33); Numbers.add(55); Numbers.add(62); // apply removeIf() method // to remove numbers divisible by 2 Numbers.removeIf(n -> (n % 2 == 0)); System.out.println("All Numbers not divisible by 2 are:"); // print list for (int i : Numbers) { System.out.println(i); } } } Output: All Numbers not divisible by 2 are: 33 55 Example 2: To demonstrate removeIf() method on Vector which contains set of Students Names and to remove all 4 char long name. Java // Java Program Demonstrate removeIf() // method of Vector import java.util.*; public class GFG { public static void main(String[] args) { // create a Vector // containing a list of string values Vector<String> students = new Vector<String>(); // Add Strings to list // each string represents student name students.add("Rama"); students.add("Mohan"); students.add("Sohan"); students.add("Rabi"); students.add("Shabbir"); // apply removeIf() method // to remove names contains 4 chars students.removeIf(n -> (n.length() == 4)); System.out.println("Students name do not contain 4 char are"); // print list for (String str : students) { System.out.println(str); } } } Output: Students name do not contain 4 char are Mohan Sohan Shabbir Example 3: To demonstrate NullpointerException in removeIf() method on Vector. Java // Java Program Demonstrate removeIf() // method of Vector import java.util.*; public class GFG { public static void main(String[] args) { // create a Vector // containing a list of string values Vector<String> students = new Vector<String>(); // Add Strings to list // each string represents student name students.add("Rama"); students.add("Mohan"); students.add("Sohan"); students.add("Rabi"); students.add("Shabbir"); try { // apply removeIf() method with null filter students.removeIf(null); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: Exception: java.lang.NullPointerException Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html#removeIf-java.util.function.Predicate- Comment More infoAdvertise with us Next Article Vector removeIf() method in Java A AmanSingh2210 Follow Improve Article Tags : Java java-basics Java-Functions Java-Vector Practice Tags : Java Similar Reads Vector forEach() method in Java The forEach() method of Vector is used to perform a given action for every element of the Iterable of Vector until all elements have been Processed by the method or an exception occurs. The operations are performed in the order of iteration if the order is specified by the method. Exceptions thrown 3 min read Vector hashCode() Method in Java The java.util.vector.hashCode() method in Java is used to get the hashcode value of this vector. Syntax: Vector.hashCode() Parameters: The method does not take any parameter. Return Value: The method returns hash code value of this Vector which is of Integer type. Below programs illustrate the Java. 2 min read 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 Like