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


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


PriorityBlockingQueue的clear()方法从此队列中删除所有元素。因此,当需要清除PriorityBlockingQueue时,可以应用此方法。

用法:

public void clear()

参数:
此方法不带参数。


返回值:
此方法不返回任何内容。

异常:
此方法不会引发任何异常。

以下示例程序旨在说明使用clear()方法从PriorityBlockingQueue中删除所有元素。

示例1:

// Java Program to Demonstrate clear() method 
// of PriorityBlockingQueue. 
  
import java.util.concurrent.PriorityBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // define capacity of PriorityBlockingQueue 
        int capacity = 15; 
  
        // create object of PriorityBlockingQueue 
        PriorityBlockingQueue<Integer> PrioBlockingQueue 
            = new PriorityBlockingQueue<Integer>(capacity); 
  
        // add numbers 
        PrioBlockingQueue.add(78758575); 
        PrioBlockingQueue.add(63447688); 
        PrioBlockingQueue.add(56434788); 
  
        // print queue after add operation 
        System.out.println("After Adding  Numbers:"); 
        System.out.println("PriorityBlockingQueue:"
                           + PrioBlockingQueue); 
  
        // remove all the elements using clear() method 
        PrioBlockingQueue.clear(); 
  
        // print queue after clear operation 
        System.out.println("\nAfter clear operation:"); 
        System.out.println("PriorityBlockingQueue:"
                           + PrioBlockingQueue); 
    } 
}
输出:
After Adding  Numbers:
PriorityBlockingQueue:[56434788, 78758575, 63447688]

After clear operation:
PriorityBlockingQueue:[]

示例2:为了说明在PriorityBlockingQueue上包含名称列表的清晰方法。

// Java Program to Demonstrate clear() method 
// of PriorityBlockingQueue. 
  
import java.util.concurrent.PriorityBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // define capacity of PriorityBlockingQueue 
        int capacity = 15; 
  
        // create object of PriorityBlockingQueue 
        PriorityBlockingQueue<String> PrioBlockingQueue 
            = new PriorityBlockingQueue<String>(capacity); 
  
        // add some names 
        PrioBlockingQueue.add("Tandrima"); 
        PrioBlockingQueue.add("Argha"); 
        PrioBlockingQueue.add("Arka"); 
  
        // print queue after add operation 
        System.out.println("List of Names:"); 
        System.out.println("PriorityBlockingQueue: "
                           + PrioBlockingQueue); 
  
        // remove all the elements using clear() method 
        PrioBlockingQueue.clear(); 
  
        // print queue after clear operation 
        System.out.println("\nAfter clearing List of names:"); 
        System.out.println("PriorityBlockingQueue:"
                           + PrioBlockingQueue); 
    } 
}
输出:
List of Names:
PriorityBlockingQueue: [Argha, Tandrima, Arka]

After clearing List of names:
PriorityBlockingQueue:[]

参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#clear–



相关用法


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