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


Java BlockingDeque removeLastOccurrence()用法及代码示例


BlockingDeque的removeLastOccurrence()方法从此双端队列中删除最后一次出现的指定元素。如果双端队列不包含元素,则它将保持不变。如果此双端队列包含指定的元素,则返回true,否则返回false。

用法:

public boolean removeLastOccurrence(Object o)

参数:此方法接受一个强制性参数o,该参数指定最后一个要从出队容器中删除的元素。


返回值:如果该元素存在并且已从Deque容器中删除,则此方法返回true,否则返回false。

注意:BlockingDeque的removeLastOccurrence()方法已从Java中的LinkedBlockingDeque类继承。

以下示例程序旨在说明BlockingDeque的removeLastOccurrence()方法:

示例1:存在元素时。

// Java Program to demonstrate removeLastOccurrence() 
// method of BlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.concurrent.BlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of BlockingDeque 
        BlockingDeque<Integer> BD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add numbers to end of BlockingDeque 
        BD.add(15); 
        BD.add(20); 
        BD.add(20); 
        BD.add(15); 
  
        // print Dequeue 
        System.out.println("Blocking Deque: " + BD); 
  
        if (BD.removeLastOccurrence(15)) 
            System.out.println("Last occurrence of 15 removed"); 
        else
            System.out.println("15 not present and not removed"); 
  
        // prints the Deque after removal 
        System.out.println("Blocking Deque: " + BD); 
    } 
}
输出:
Blocking Deque: [15, 20, 20, 15]
Last occurrence of 15 removed
Blocking Deque: [15, 20, 20]

示例2:当元素不存在时。

// Java Program to demonstrate removeLastOccurrence() 
// method of BlockingDeque 
  
import java.util.concurrent.BlockingDeque; 
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of BlockingDeque 
        BlockingDeque<Integer> BD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add numbers to end of BlockingDeque 
        BD.add(15); 
        BD.add(20); 
        BD.add(20); 
        BD.add(15); 
  
        // print Deque 
        System.out.println("Blocking Deque: " + BD); 
  
        if (BD.removeLastOccurrence(10)) 
            System.out.println("Last occurrence of 10 removed"); 
        else
            System.out.println("10 not present and not removed"); 
  
        // prints the Deque after removal 
        System.out.println("Blocking Deque: " + BD); 
    } 
}
输出:
Blocking Deque: [15, 20, 20, 15]
10 not present and not removed
Blocking Deque: [15, 20, 20, 15]

参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#removeLastOccurrence(java.lang.Object)



相关用法


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