PriorityBlockingQueue size() method in Java Last Updated : 26 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The size() method of PriorityBlockingQueue is used to find the present size of the queue. It returns the number of elements in the collection. If the collection contains more than Integer.MAX_VALUE elements, then this method returns Integer.MAX_VALUE. Syntax: public int size() Return Value: This method returns the number of elements present in this PriorityBlockingQueue Below are the program to illustrate size() method of PriorityBlockingQueue: Program 1: Java // Java program to demonstrate // size() method 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("1"); pbq.put("2"); pbq.put("3"); pbq.put("4"); // print queue System.out.println("Queue: " + pbq); System.out.println("Queue Size: " + pbq.size()); } } Output: Queue: [1, 2, 3, 4] Queue Size: 4 Program 2: To demonstrate size() for dynamically changing queue. Java // Java program to demonstrate // size() method 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("1"); pbq.put("2"); pbq.put("3"); pbq.put("4"); // print queue System.out.println("Queue: " + pbq); System.out.println("Queue Size: " + pbq.size()); // remove 2 boolean res = pbq.remove("2"); System.out.println("\n2 removed: " + res); // print queue System.out.println("Queue: " + pbq); System.out.println("Queue Size: " + pbq.size()); // add 5 pbq.put("5"); // print queue System.out.println("\n5 added"); System.out.println("Queue: " + pbq); System.out.println("Queue Size: " + pbq.size()); } } Output: Queue: [1, 2, 3, 4] Queue Size: 4 2 removed: true Queue: [1, 4, 3] Queue Size: 3 5 added Queue: [1, 4, 3, 5] Queue Size: 4 Comment More infoAdvertise with us Next Article PriorityBlockingQueue size() method in Java C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-PriorityBlockingQueue +1 More Practice Tags : JavaJava-Collections Similar Reads PriorityBlockingQueue take() method in Java The take() method of PriorityBlockingQueue returns head of the queue after removing it. If queue is empty, then this method will wait until an element becomes available. Syntax: public E take() throws InterruptedException Returns: This method returns value at the head of this PriorityBlockingQueue. 2 min read PriorityBlockingQueue put() method in Java The put(E e) method of PriorityBlockingQueue is used to add an element into this queue. This method inserts the specified element into this priority queue. Since the queue is unbounded, this method will be never be blocked.Syntax: public void put(E e) Parameter: This method accepts a mandatory param 2 min read PriorityBlockingQueue toString() method in Java The toString() method of PriorityBlockingQueue returns String representation of this PriorityBlockingQueue showing details of element contained by PriorityBlockingQueue. The string of PriorityBlockingQueue contains elements of PriorityBlockingQueue in the same order returned by its iterator, enclose 2 min read PriorityBlockingQueue remove() method in Java The remove(Object o) method of PriorityBlockingQueue is used to delete an element from this queue. This method removes a single instance of the element passed as the parameter, if it is present. It returns true if and only if the element was removed, else it returned false. Syntax: public boolean re 2 min read PriorityBlockingQueue spliterator() method in Java The spliterator() method of PriorityBlockingQueue returns a Spliterator of the same elements as PriorityBlockingQueue. The returned iterator is weakly consistent. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too.Spliterator is better way to traverse o 2 min read Like