Java PriorityQueue forEach() Method



Description

The Java PriorityQueue forEach(E action) is used to performs a given action for each element of the Iterable until all elements are processed exception occurs during action. In case, an order is specified then actions are performed in the given order. In case of exception, the exception is relayed to the caller.

Declaration

Following is the declaration for java.util.PriorityQueue.forEach() method

public void forEach​(Consumer<? super E>> action)

Parameters

action − The action to be performed for each element.

Exception

NullPointerException − if the specified action is null.

Iterating PriorityQueue of Ints using forEach() method Example

The following example shows the usage of Java PriorityQueue forEach(action) method to iterate and print Integers. We're adding couple of Integers to the PriorityQueue object using add() method calls per element and then print each element using forEach() to show the elements added.

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<Integer> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(1);
      queue.add(2);
      queue.add(3);
      queue.add(4);
      queue.add(5);

      // let us print all the elements available in queue
      queue.forEach(s -> { System.out.println(s);});
   }
}

Output

Let us compile and run the above program, this will produce the following result −

1
2
3
4
5

Iterating PriorityQueue of Strings using forEach() Method Example

The following example shows the usage of Java PriorityQueue forEach(action) method to iterate and print Strings. We're adding couple of Strings to the PriorityQueue object using add() method calls per element and then print each element using forEach() to show the elements added.

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<String> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add("A");
      queue.add("B");
      queue.add("C");
      queue.add("D");
      queue.add("E");

      // let us print all the elements available in queue
      queue.forEach(s -> { System.out.println(s);});
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A
B
C
D
E

Iterating PriorityQueue of Objects using forEach() method Example

The following example shows the usage of Java PriorityQueue forEach(action) method to iterate and print Student objects. We're adding couple of Student objects to the PriorityQueue object using add() method calls per element and then print each element using forEach() to show the elements added.

package com.tutorialspoint;

import java.util.PriorityQueue;
import java.util.Deque;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<Student> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(new Student(1, "Julie"));
      queue.add(new Student(2, "Robert"));
      queue.add(new Student(3, "Adam"));

      // let us print all the elements available in queue
      queue.forEach(s -> { System.out.println(s);});
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

Output

Let us compile and run the above program, this will produce the following result −

[ 1, Julie ]
[ 2, Robert ]
[ 3, Adam ]
java_util_priorityqueue.htm
Advertisements