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


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


PriorityBlockingQueue的剩余容量方法用于检查可以在此队列中插入多少元素。但是由于PriorityBlockingQueue是无界的,因此此方法始终返回Integer.MAX_VALUE,因为PriorityBlockingQueue不受容量限制。

用法:

public int remainingCapacity()

返回值:始终为Integer.MAX_VALUE


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

示例1:

// Java Program Demonstrate remainingCapacity() 
// method of PriorityBlockingQueue 
  
import java.util.concurrent.PriorityBlockingQueue; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // define capacity of PriorityBlockingQueue 
        int capacityOfQueue = 7; 
  
        // create object of PriorityBlockingQueue 
        PriorityBlockingQueue<Integer> pbq 
            = new PriorityBlockingQueue<Integer>(capacityOfQueue); 
  
        // Add element to PriorityBlockingQueue 
        pbq.put(1); 
        pbq.put(2); 
        pbq.put(3); 
        pbq.put(4); 
  
        // find remaining Capacity  of pbq 
        // using remainingCapacity() method 
  
        // The initial capacity was set to 7 
        // by passing as parameter in contructor 
        // But this method will return Integer.MAX_VALUE 
        int remainingCapacity = pbq.remainingCapacity(); 
  
        // print result 
        System.out.println("Queue:  " + pbq); 
        System.out.println("Remaining Capacity: " + remainingCapacity); 
    } 
}
输出:
Queue:  [1, 2, 3, 4]
Remaining Capacity: 2147483647

示例2:演示使用String的remainingCapacity()

// Java Program Demonstrate remainingCapacity() 
// 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"); 
  
        // find remaining Capacity  of pbq 
        // using remainingCapacity() method 
        int remainingCapacity = pbq.remainingCapacity(); 
  
        // print result 
        System.out.println("Queue:  " + pbq); 
        System.out.println("Remaining Capacity: " + remainingCapacity); 
    } 
}
输出:
Queue:  [A Computer, Portal, Geeks, forGeeks]
Remaining Capacity: 2147483647


相关用法


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