Vector removeIf() method in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Students name do not contain 4 char are
Mohan
Sohan
Shabbir

Example 3: To demonstrate NullpointerException in removeIf() method on Vector.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Exception: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html#removeIf-java.util.function.Predicate-



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.