Java PriorityBlockingQueue forEach() Method Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report forEach() method of PriorityBlockingQueue in Java is used to iterate over all the elements in the PriorityBlockingQueue and perform a specific task. This method processes the elements sequentially in the order they are present in the queue. The PriorityBlockingQueue implements the Queue interface, which provides this functionality.Example 1: The below Java program demonstrates the use of the forEach() method to iterate over and print each integer element in the PriorityBlockingQueue. Java // Iterate over the elements of the // queue and print the integer import java.util.concurrent.PriorityBlockingQueue; class Geeks { public static void main(String[] args) { // Creating a PriorityBlockingQueue PriorityBlockingQueue q = new PriorityBlockingQueue(); // use add() to insert elements in the queue q.add(20); q.add(10); q.add(30); // Using forEach to print each element of the queue System.out.println( "Elements in PriorityBlockingQueue:"); q.forEach(e -> System.out.println(e)); } } OutputElements in PriorityBlockingQueue: 10 20 30 Syntaxvoid forEach(Consumer action)Parameter: action - A consumer defines what action should be performed on each element.Example 2: The below Java program demonstrates the use of forEach() method to iterate over and print each String element in the queue. Java // Iterate over the elements of the // queue and print the string import java.util.concurrent.PriorityBlockingQueue; class Geeks { public static void main(String[] args) { // Creating a PriorityBlockingQueue PriorityBlockingQueue<String> q = new PriorityBlockingQueue<>(); // use add() method to insert elements in the queue q.add("A"); q.add("D"); q.add("C"); q.add("B"); q.add("E"); // Using forEach to print each element of the queue System.out.println( "Elements in PriorityBlockingQueue:"); q.forEach(s -> { System.out.println(s); }); } } OutputElements in PriorityBlockingQueue: A B C D E Comment More infoAdvertise with us Next Article Java PriorityBlockingQueue forEach() Method J juhisrivastav Follow Improve Article Tags : Java Java-PriorityBlockingQueue Practice Tags : Java Similar Reads PriorityBlockingQueue clear() method in Java The clear() method of PriorityBlockingQueue removes all the elements from this queue. Therefore this method can be applied when it is required to clear the PriorityBlockingQueue. Syntax: public void clear() Parameter: This method takes no parameters. Returns: This method returns nothing. Exception: 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 PriorityBlockingQueue drainTo() method in Java The drainTo(Collection col) method of PriorityBlockingQueue removes all available elements from this LinkedBlocking Queue and adds them to the given collection passed as a parameter. drainTo(Collection<? super E> col) The drainTo(Collection<? super E> col) method of PriorityBlockingQueue 5 min read PriorityBlockingQueue iterator() method in Java The iterator() method of PriorityBlockingQueue class Returns an iterator over the elements in this queue. The elements returned from this method do not follow any order. The returned iterator is weakly consistent. Syntax: public Iterator iterator() Parameter: This method does not take any parameter. 2 min read 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 Like