Java中的 PriorityBlockingQueue remainingCapacity() 方法
PriorityBlockingQueue的剩余容量方法用于检查可以向该队列中插入多少元素。但由于 PriorityBlockingQueue 是无界的,因此该方法总是返回Integer.MAX_VALUE ,因为 PriorityBlockingQueue 不受容量限制。
句法:
public int remainingCapacity()
返回值: Integer.MAX_VALUE 总是
下面的程序说明了 PriorityBlockingQueue 中的剩余容量()方法:
方案一:
Java
// 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 pbq
= new PriorityBlockingQueue(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 constructor
// 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);
}
}
Java
// 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 pbq
= new PriorityBlockingQueue();
// 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: [1, 2, 3, 4]
Remaining Capacity: 2147483647
程序 2:使用 String 演示剩余容量()
Java
// 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 pbq
= new PriorityBlockingQueue();
// 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