當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。