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


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


PriorityBlockingQueue的remove(Object o)方法用于从此队列中删除元素。如果存在,此方法将删除作为参数传递的元素的单个实例。
当且仅当元素被删除时,它才返回true;否则,它返回false。

用法:

public boolean remove(Object o)

参数:此方法接受必需参数o,该参数是要从此队列中删除的元素(如果存在)。


返回值:如果指定的元素已成功删除,则此方法返回true。否则,此方法返回false。

下面的程序说明PriorityBlockingQueue中的remove()方法:

示例1:

// Java Program Demonstrate remove() 
// method of PriorityBlockingQueue 
  
import java.util.concurrent.PriorityBlockingQueue; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create object of PriorityBlockingQueue 
        PriorityBlockingQueue<Integer> pbq 
            = new PriorityBlockingQueue<Integer>(); 
  
        // Add element to PriorityBlockingQueue 
        pbq.put(1); 
        pbq.put(2); 
        pbq.put(3); 
        pbq.put(4); 
  
        // print queue 
        System.out.println("Queue: " + pbq); 
  
        // remove 2 
        boolean res = pbq.remove(2); 
        System.out.println("\n2 removed: " + res); 
  
        // print queue 
        System.out.println("Queue:  " + pbq); 
  
        // remove 5 
        res = pbq.remove(5); 
        System.out.println("\n5 removed: " + res); 
  
        // print queue 
        System.out.println("Queue:  " + pbq); 
    } 
}
输出:
Queue: [1, 2, 3, 4]

2 removed: true
Queue:  [1, 4, 3]

5 removed: false
Queue:  [1, 4, 3]
// Java Program Demonstrate remove() 
// method of PriorityBlockingQueue 
  
import java.util.concurrent.PriorityBlockingQueue; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create object of PriorityBlockingQueue 
        PriorityBlockingQueue<String> pbq 
            = new PriorityBlockingQueue<String>(); 
  
        // Add element to PriorityBlockingQueue 
        pbq.put("Geeks"); 
        pbq.put("forGeeks"); 
        pbq.put("A Computer"); 
        pbq.put("Portal"); 
  
        // print queue 
        System.out.println("Queue: " + pbq); 
  
        // remove Geeks 
        boolean res = pbq.remove("Geeks"); 
        System.out.println("\nGeeks removed: " + res); 
  
        // print queue 
        System.out.println("Queue:  " + pbq); 
  
        // remove SandeepJain 
        res = pbq.remove("SandeepJain"); 
        System.out.println("\nSandeepJain removed: " + res); 
  
        // print queue 
        System.out.println("Queue:  " + pbq); 
    } 
}
输出:
Queue: [A Computer, Portal, Geeks, forGeeks]

Geeks removed: true
Queue:  [A Computer, Portal, forGeeks]

SandeepJain removed: false
Queue:  [A Computer, Portal, forGeeks]


相关用法


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