removeIf(Predicate): This method is available in java.util.Vector class of Java.
Syntax:
boolean java.util.Vector.removeIf(Predicate<? super K> filter)
This method takes one argument. This method removes all of the elements of this collection that satisfy the given predicate.
Parameters: One parameter is required for this method.
filter: a predicate that returns true for elements to be removed.
Returns: true if any elements were removed.
Throws:
NullPointerException - if the specified filter is null
Approach 1: When no exception
Java
import java.util.Vector;import java.util.function.Predicate;public class VectorremoveIf {public static void main(String[] args) {Vector<Integer> vector = new Vector<>();vector.add(10);vector.add(5);vector.add(4);vector.add(13);Predicate<? super Integer> filter = k -> (k > 1);System.out.println(vector.removeIf(filter));}}
Output:
true
Approach 2: NullPointerException
Java
import java.util.Vector;import java.util.function.Predicate;public class VectorremoveIf {public static void main(String[] args) {Vector<Integer> vector = new Vector<>();vector.add(10);vector.add(5);vector.add(4);vector.add(13);Predicate<? super Integer> filter = null;System.out.println(vector.removeIf(filter));}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.Vector.removeIf(Vector.java:963)
No comments:
Post a Comment