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


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


LinkedBlockingDeque的removeIf()方法从此LinkedBlockingDeque中删除满足指定条件的元素。

用法:

public boolean removeIf (Predicate<? super E> filter)

参数:此方法接受强制性参数过滤器,该过滤器是基于从此双端队列中删除哪些元素的谓词值。


返回值:如果更改了LinkedBlockingDeque,则此方法返回布尔值,例如true。否则,此方法返回false。

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

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

示例1:

// Java Program Demonstrate removeIf() 
// method of LinkedBlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // Create object of LinkedBlockingDeque 
        LinkedBlockingDeque<Integer> LBD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add numbers to end of LinkedBlockingDeque 
        LBD.add(7855642); 
        LBD.add(35658786); 
        LBD.add(5278367); 
        LBD.add(74381793); 
  
        // Print Deque 
        System.out.println("Linked Blocking Deque: "
                           + LBD); 
  
        // If a number in the List is 
        // divisible by 3, then remove it 
        LBD.removeIf(number -> number % 3 == 0); 
  
        System.out.println("Linked Blocking Deque: "
                           + LBD); 
    } 
}
输出:
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Linked Blocking Deque: [7855642, 5278367]

示例2:

// Java Program Demonstrate removeIf() 
// method of LinkedBlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // Create object of LinkedBlockingDeque 
        LinkedBlockingDeque<Integer> LBD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add numbers to end of LinkedBlockingDeque 
        LBD.add(7855642); 
        LBD.add(35658786); 
        LBD.add(5278367); 
        LBD.add(74381793); 
  
        // Print Dequeue 
        System.out.println("Linked Blocking Deque: "
                           + LBD); 
  
        try { 
            // if the predicate is null, 
            // then it will throw NullPointerException 
            LBD.removeIf(null); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
输出:
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
java.lang.NullPointerException

参考: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedBlockingDeque.html#removeIf-java.util.function.Predicate-



相关用法


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