Java PriorityBlockingQueue forEach() Method Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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 J juhisrivastav Follow Improve Article Tags : Java Java-PriorityBlockingQueue Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial15+ min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like