Java PriorityQueue 的 forEach() 方法



描述

Java PriorityQueue forEach(E action) 用於對 Iterable 的每個元素執行給定操作,直到處理完所有元素或操作過程中發生異常。如果指定了順序,則按指定順序執行操作。如果發生異常,則將異常傳遞給呼叫者。

宣告

以下是java.util.PriorityQueue.forEach() 方法的宣告

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

引數

action − 對每個元素執行的操作。

異常

NullPointerException − 如果指定的操作為null

使用 forEach() 方法迭代整數 PriorityQueue 的示例

以下示例演示了 Java PriorityQueue forEach(action) 方法的使用,用於迭代和列印整數。我們使用 add() 方法為每個元素向 PriorityQueue 物件新增幾個整數,然後使用 forEach() 列印每個元素以顯示新增的元素。

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);});
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

1
2
3
4
5

使用 forEach() 方法迭代字串 PriorityQueue 的示例

以下示例演示了 Java PriorityQueue forEach(action) 方法的使用,用於迭代和列印字串。我們使用 add() 方法為每個元素向 PriorityQueue 物件新增幾個字串,然後使用 forEach() 列印每個元素以顯示新增的元素。

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);});
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

A
B
C
D
E

使用 forEach() 方法迭代物件 PriorityQueue 的示例

以下示例演示了 Java PriorityQueue forEach(action) 方法的使用,用於迭代和列印 Student 物件。我們使用 add() 方法為每個元素向 PriorityQueue 物件新增幾個 Student 物件,然後使用 forEach() 列印每個元素以顯示新增的元素。

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;
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

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