当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LinkedTransferQueue removeIf()用法及代码示例


java.util.concurrent.LinkedTransferQueue的removeIf()方法是Java的内置函数,用于删除该队列中满足给定谓词过滤器的所有元素,该谓词过滤器作为参数传递给该方法。

用法:

public boolean removeIf(Predicate filter)

参数:此方法采用代表谓词的参数过滤器,该谓词定义了要删除元素的过滤标准。


返回值:此方法返回一个布尔值,描述是否已删除某些指定元素。如果谓词返回true,并且元素已被删除,则返回True。

异常:如果指定的过滤器为null,则此方法将引发NullPointerException。

以下示例程序旨在说明LinkedTransferQueue类的removeIf()函数:

示例1:

// Java code to illustrate 
// removeIf() method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of LinkedTransferQueue 
        LinkedTransferQueue<Integer> LTQ 
            = new LinkedTransferQueue<Integer>(); 
  
        // Add numbers to end of LinkedTransferQueue 
        // using add() method 
        LTQ.add(6); 
        LTQ.add(3); 
        LTQ.add(5); 
        LTQ.add(15); 
        LTQ.add(20); 
  
        // Print the LTQ 
        System.out.println("Linked Transfer Queue initially: "
                           + LTQ); 
  
        // Remove all multiple of 3 
        // using removeIf() method 
        if (LTQ.removeIf(n -> (n % 3 == 0))) 
            System.out.println("Multiples of 3 removed."); 
        else
            System.out.println("No element removed."); 
  
        // Print the LTQ 
        System.out.println("Current Linked Transfer Queue: "
                           + LTQ); 
    } 
}
输出:
Linked Transfer Queue initially: [6, 3, 5, 15, 20]
Multiples of 3 removed.
Current Linked Transfer Queue: [5, 20]

示例2:演示NullPointerException

// Java code to illustrate 
// removeIf() method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of LinkedTransferQueue 
        LinkedTransferQueue<Integer> LTQ 
            = new LinkedTransferQueue<Integer>(); 
  
        // Add numbers to end of LinkedTransferQueue 
        // using add() method 
        LTQ.add(6); 
        LTQ.add(3); 
        LTQ.add(5); 
        LTQ.add(15); 
        LTQ.add(20); 
  
        // Print the LTQ 
        System.out.println("Linked Transfer Queue initially: "
                           + LTQ); 
  
        try { 
            // Remove all multiple of 3 
            // using removeIf() method 
            System.out.println("Trying to remove null."); 
            LTQ.removeIf(null); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
输出:
Linked Transfer Queue initially: [6, 3, 5, 15, 20]
Trying to remove null.
java.lang.NullPointerException

参考: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/LinkedTransferQueue.html#removeIf(java.util.function.Predicate)



相关用法


注:本文由纯净天空筛选整理自ProgrammerAnvesh大神的英文原创作品 LinkedTransferQueue removeIf() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。