Skip to content
Related Articles

Related Articles

PriorityBlockingQueue remainingCapacity() method in Java

View Discussion
Improve Article
Save Article
  • Last Updated : 07 May, 2021
View Discussion
Improve Article
Save Article

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

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!