PriorityBlockingQueue take() method in Java Last Updated : 26 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Exception: This method throws InterruptedException, if interrupted while waiting for an element to become available. Below programs illustrate take() method of PriorityBlockingQueue: Example 1: To demonstrate take() method on PriorityBlockingQueue which contains a list of numbers. Java // Java Program Demonstrate take() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of PriorityBlockingQueue PriorityBlockingQueue<Integer> PrioQueue = new PriorityBlockingQueue<Integer>(); // Add numbers to PriorityBlockingQueue PrioQueue.put(7855642); PrioQueue.put(35658786); PrioQueue.put(5278367); PrioQueue.put(74381793); // before removing print queue System.out.println("Queue: " + PrioQueue); // Apply take() method int head = PrioQueue.take(); // Print head of queue using take() method System.out.println("Head of PriorityBlockingQueue" + " using take(): " + head); System.out.print("After removing head, Queue: " + PrioQueue); } } Output: Queue: [5278367, 35658786, 7855642, 74381793] Head of PriorityBlockingQueue using take(): 5278367 After removing head, Queue: [7855642, 35658786, 74381793] Example 2: To demonstrate take() method on PriorityBlockingQueue which contains String Java // Java Program Demonstrate take() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of PriorityBlockingQueue // which contains Strings PriorityBlockingQueue<String> names = new PriorityBlockingQueue<String>(); // Add string names.add("Geeks"); names.add("forGeeks"); names.add("A computer portal"); // print list of names System.out.println(names); // Apply take() method String head = names.take(); // Print head of queue using take() method System.out.println("Head of Queue: " + head); System.out.print("After removing head, Queue: " + names); } } Output: [A computer portal, forGeeks, Geeks] Head of Queue: A computer portal After removing head, Queue: [Geeks, forGeeks] Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#take-- Comment More infoAdvertise with us Next Article PriorityBlockingQueue take() method in Java A AmanSingh2210 Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-PriorityBlockingQueue +1 More Practice Tags : JavaJava-Collections Similar Reads 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 size() method in Java 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 met 2 min read PriorityBlockingQueue toArray() method in Java toArray() The toArray method of PriorityBlockingQueue is used to create an array having the same elements as that of this PriorityBlockingQueue, in proper sequence. Actually, this method copies all the element from the PriorityBlockingQueue to a new array. This method behaves as a bridge between arr 4 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 add() Method in Java The add(E e) method of PriorityBlockingQueue inserts the element passed as a parameter to the method at the tail of this PriorityBlockingQueue. This method returns true if the adding of the element is successful. Else it returns false. Syntax: public boolean add(E e) Parameter: This method takes a m 2 min read Like