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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Program 2: To demonstrate remainingCapacity() using String

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.