📜  Java 8 | Java中的 ArrayDeque removeIf() 方法及示例

📅  最后修改于: 2022-05-13 01:55:32.138000             🧑  作者: Mango

Java 8 | Java中的 ArrayDeque removeIf() 方法及示例

ArrayDeque 的removeIf()方法用于从ArrayDeque中删除所有满足作为参数传递给该方法的给定谓词过滤条件的元素。如果从 Vector 中删除了某些元素,则此方法返回 true。

Java 8 有一个重要的内置函数接口,即 Predicate。谓词或条件检查函数,它检查给定输入的给定条件,并返回一个布尔结果,指示是否满足条件。

句法:

public boolean removeIf(Predicate filter)

参数:此方法采用一个参数过滤器,该过滤器表示一个谓词,该谓词对于要删除的元素返回 true。

返回:如果谓词返回 true 并且删除了某些元素,则此方法返回True

异常:如果指定的过滤器为空,此方法将抛出NullPointerException

下面的程序说明了 ArrayDeque 的 removeIf() 方法:

示例 1:演示 ArrayDeque 上的 removeIf() 方法,该方法包含一组 String 并删除以 A 开头的字符串。

// Java Program Demonstrate removeIf()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque
        // containing a list of string values
        ArrayDeque students = new ArrayDeque();
  
        // Add Strings to list
        // each string represents student name
        students.add("Aman");
        students.add("Sanjeet");
        students.add("Amar");
        students.add("Rabi");
        students.add("Shabbir");
  
        // apply removeIf() method
        // to remove names which start with A
        students.removeIf(n -> (n.charAt(0) == 'A'));
  
        System.out.println("Students name do not starts with A");
  
        // print list
        for (String str : students) {
            System.out.println(str);
        }
    }
}
输出:
Students name do not starts with A
Sanjeet
Rabi
Shabbir

示例 2:演示 ArrayDeque 上的 removeIf() 方法,该方法包含一组学生对象,以删除所有低于 40 分的学生。

// Java Program Demonstrate removeIf()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque
        // containing a list of Student objects
        ArrayDeque students = new ArrayDeque();
  
        // Add student object to list
        students.add(new student("Aman", 78));
        students.add(new student("Amar", 79));
        students.add(new student("Suraj", 38));
        students.add(new student("Raju", 22));
        students.add(new student("Ankit", 76));
        students.add(new student("Sanju", 62));
  
        // apply removeIf() method
        // to remove students who scores below 40
        students.removeIf(n -> (n.marks <= 40));
  
        System.out.println("Students list who score above 40");
  
        // print list
        for (student str : students) {
            System.out.println(str.name + ", " + str.marks);
        }
    }
}
  
// create student class
class student {
  
    public String name;
    public int marks;
  
    student(String name, int marks)
    {
        this.name = name;
        this.marks = marks;
    }
}
输出:
Students list who score above 40
Aman, 78
Amar, 79
Ankit, 76
Sanju, 62

示例 3:演示 removeIf() 方法中的 NullpointerException。

// Java Program Demonstrate removeIf()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque
        // containing a list of string values
        ArrayDeque students = new ArrayDeque();
  
        // Add Strings to list
        // each string represents student name
        students.add("Aman");
        students.add("Sanjeet");
        students.add("Amar");
        students.add("Rabi");
        students.add("Shabbir");
  
        try {
            // apply removeIf() method with null filter
            students.removeIf(null);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Exception: java.lang.NullPointerException

参考: https: Java Java 函数)