Java Vector - removeIf() Method
The java.util.Vector.removeIf() method is used to remove all of the elements of the vector that satisfy the given predicate.
Syntax
public boolean removeIf(Predicate<? super E> filter)
Here, E is the type of element maintained by the container.
Parameters
filter |
Specify filter, a predicate which returns true for elements to be removed. |
Return Value
Returns true if any elements were removed.
Exception
Throws NullPointerException, if the specified filter is null.
Example:
In the example below, the java.util.Vector.removeIf() method is used to remove all of the elements of the vector which are divisible by 10.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a vector Vector<Integer> MyVector = new Vector<Integer>(); //populating MyVector MyVector.add(5); MyVector.add(10); MyVector.add(15); MyVector.add(20); MyVector.add(25); MyVector.add(30); //printing MyVector System.out.println("Before removeIf, MyVector contains: " + MyVector); //remove all elements which are divisible by 10 MyVector.removeIf((n) -> (n % 10 == 0)); //printing MyVector System.out.println("After removeIf, MyVector contains: " + MyVector); } }
The output of the above code will be:
Before removeIf, MyVector contains: [5, 10, 15, 20, 25, 30] After removeIf, MyVector contains: [5, 15, 25]
❮ Java.util - Vector