- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Vector removeIf() Method
Description
The Java Vector removeIf(Predicate<? super E> filter) method retrieves and removes all the elements of this Vector that satisfy the given predicate. In case of exception, the exception is relayed to the caller.
Declaration
Following is the declaration for java.util.Vector.removeIf(filter) method
public boolean removeIf(Predicate<? super E> filter)
Parameters
filter − a predicate which returns true for elements to be removed.
Return Value
true if any elements were removed.
Exception
NullPointerException − if the specified filter is null
Removing a Element of a Vector of Integer by Condition Example
The following example shows the usage of Java Vector removeIf(filter) method. We're creating an Vector of Integers, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As Vector is modified it is printed to check if even numbers are removed or not.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Integer> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(1); vector.add(2); vector.add(3); vector.add(4); vector.add(5); // let us print all the elements available in vector System.out.println("Vector = " + vector); // it will remove even numbers from the vector vector.removeIf(i -> i%2 == 0); // let us print all the elements available in vector again System.out.println("Vector = " + vector); } }
Output
Let us compile and run the above program, this will produce the following result −
Vector = [1, 2, 3, 4, 5] Vector = [1, 3, 5]
Removing a Element of a Vector of String by Condition Example
The following example shows the usage of Java Vector removeIf(filter) method. We're creating an Vector of String, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As Vector is modified it is printed to check if even numbers are removed or not.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<String> vector = new Vector<>(); // use add() method to add elements in the vector vector.add("A"); vector.add("BB"); vector.add("C"); vector.add("DD"); vector.add("E"); // let us print all the elements available in vector System.out.println("Vector = " + vector); // it will remove single length string vector.removeIf(i -> i.length() == 1); // let us print all the elements available in vector again System.out.println("Vector = " + vector); } }
Output
Let us compile and run the above program, this will produce the following result −
Vector = [A, BB, C, DD, E] Vector = [BB, DD]
Removing a Element of a Vector of Object by Condition Example
The following example shows the usage of Java Vector removeIf(filter) method. We're creating an Vector of Student objects, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As Vector is modified it is printed to check if even numbers are removed or not.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Student> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(new Student(1, "Julie")); vector.add(new Student(2, "Robert")); vector.add(new Student(3, "Adam")); vector.add(new Student(4, "Jene")); vector.add(new Student(5, "Jacob")); // let us print all the elements available in vector System.out.println("Vector = " + vector); // it will remove if roll no is 2 vector .removeIf(i -> i.rollNo == 2); // let us print all the elements available in vector again System.out.println("Vector = " + vector); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
Output
Let us compile and run the above program, this will produce the following result −
Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]] Vector = [[ 1, Julie ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]]
To Continue Learning Please Login
Login with Google