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


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


BlockingDeque的remove()方法删除了Deque容器的头部。如果Deque容器为空,则此方法引发NoSuchElementException。如果在参数中传递了元素,则它将删除给定元素(如果存在于双端队列中)。

用法:

public E remove() or boolean remove(element)

参数:此方法接受一个临时参数element ,该元素将从BlockingDeque中删除


返回值:如果传递了参数,则此方法返回true或false,否则不返回任何内容。如果元素在双端队列中存在,则返回true,否则返回false。

异常:如果Deque为空,则该函数将引发NoSuchElementException。

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

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

示例1:

// Java Program Demonstrate remove() 
// 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(7855642); 
        BD.add(35658786); 
        BD.add(5278367); 
        BD.add(74381793); 
  
        // print Deque 
        System.out.println("Blocking Deque: " + BD); 
  
        // removes the front element 
        BD.remove(); 
        System.out.println("Blocking Deque: " + BD); 
  
        // removes the element 
        BD.remove(5278367); 
        System.out.println("Blocking Deque: " + BD); 
    } 
}
输出:
Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Blocking Deque: [35658786, 5278367, 74381793]
Blocking Deque: [35658786, 74381793]

示例2:

// Java Program Demonstrate remove() 
// 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(7855642); 
        BD.add(35658786); 
        BD.add(5278367); 
        BD.add(74381793); 
  
        // print Deque 
        System.out.println("Blocking Deque: " + BD); 
  
        BD.clear(); 
  
        // removes the front element 
        BD.remove(); 
        System.out.println("Blocking Deque: " + BD); 
  
        // removes the element 
        BD.remove(5278367); 
        System.out.println("Blocking Deque: " + BD); 
    } 
}

输出:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.concurrent.LinkedBlockingDeque.removeFirst(LinkedBlockingDeque.java:453)
    at java.util.concurrent.LinkedBlockingDeque.remove(LinkedBlockingDeque.java:672)
    at GFG.main(GFG.java:29)

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



相关用法


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