- Java.util 包类
- Java.util - 首页
- 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 包额外内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
Java Vector removeIf() 方法
描述
Java Vector removeIf(Predicate<? super E> filter) 方法检索并删除此 Vector 中满足给定谓词的所有元素。如果发生异常,则将异常转发给调用者。
声明
以下是 java.util.Vector.removeIf(filter) 方法的声明
public boolean removeIf(Predicate<? super E> filter)
参数
filter − 一个谓词,对于要删除的元素返回 true。
返回值
如果删除了任何元素,则返回 true。
异常
NullPointerException − 如果指定的过滤器为 null
根据条件删除整数向量的元素示例
以下示例演示了 Java Vector removeIf(filter) 方法的使用。我们创建一个整数向量,添加一些元素,打印它,然后使用 removeIf(filter) 方法删除偶数。由于 Vector 已修改,因此会打印它以检查是否删除了偶数。
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); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Vector = [1, 2, 3, 4, 5] Vector = [1, 3, 5]
根据条件删除字符串向量的元素示例
以下示例演示了 Java Vector removeIf(filter) 方法的使用。我们创建一个字符串向量,添加一些元素,打印它,然后使用 removeIf(filter) 方法删除偶数。由于 Vector 已修改,因此会打印它以检查是否删除了偶数。
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); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Vector = [A, BB, C, DD, E] Vector = [BB, DD]
根据条件删除对象向量的元素示例
以下示例演示了 Java Vector removeIf(filter) 方法的使用。我们创建一个 Student 对象向量,添加一些元素,打印它,然后使用 removeIf(filter) 方法删除偶数。由于 Vector 已修改,因此会打印它以检查是否删除了偶数。
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); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]] Vector = [[ 1, Julie ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]]
java_util_vector.htm
广告