Monday, November 18, 2024
Google search engine
HomeLanguagesJavaPriorityBlockingQueue remainingCapacity() method in Java

PriorityBlockingQueue remainingCapacity() method in Java

The remainingCapacity method of PriorityBlockingQueue is used to check how much more elements can be inserted into this queue. But since the PriorityBlockingQueue is unbounded, this method always returns Integer.MAX_VALUE because a PriorityBlockingQueue is not capacity constrained.
Syntax: 
 

public int remainingCapacity()

Return Value: Integer.MAX_VALUE always
Below programs illustrate remainingCapacity() method in PriorityBlockingQueue:
Program 1:
 

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<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 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);
    }
}


Output: 

Queue:  [1, 2, 3, 4]
Remaining Capacity: 2147483647

 

Program 2: To demonstrate remainingCapacity() using 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<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);
    }
}


Output: 

Queue:  [A Computer, Portal, Geeks, forGeeks]
Remaining Capacity: 2147483647

 

RELATED ARTICLES

Most Popular

Recent Comments