HOWTO · Java
Java 最大優先順序佇列
本教程演示如何從 Java 中的優先順序佇列中獲取最大值。
本頁內容
優先順序佇列是 java 中的一種資料結構,其中元素根據其自然狀態存在,而不是根據先進先出的順序。元素也可以根據優先佇列中使用的比較器進行排序。
本教程演示了優先順序佇列的使用以及如何從優先順序佇列中獲取最大值。
Java 中優先順序佇列的使用
如上所述,元素以其自然狀態存在於優先順序佇列中。讓我們看一個例子。
程式碼:
package delftstack;
import java.util.*;
public class Priority_Queue {
public static void main(String args[]) {
PriorityQueue<String> delftstack_queue = new PriorityQueue<String>();
// Add the values to the priority queue
delftstack_queue.add("delftstack3");
delftstack_queue.add("delftstack2");
delftstack_queue.add("delftstack1");
delftstack_queue.add("delftstack4");
delftstack_queue.add("delftstack5");
delftstack_queue.add("delftstack6");
// head of the PriorityQueue
System.out.println(
"Head of the PriorityQueue, The minimum value: " + delftstack_queue.element());
// All Elements of the Priority Queue
System.out.println("\nAll PriorityQueue Elements:");
Iterator demo_iterator = delftstack_queue.iterator();
while (demo_iterator.hasNext()) {
System.out.print(demo_iterator.next() + " ");
}
}
}
上面的程式碼將首先列印優先順序佇列的頭部,這將是最小值,並列印所有元素。
輸出:
Head of the PriorityQueue, The minimum value: delftstack1
All PriorityQueue Elements:
delftstack1 delftstack3 delftstack2 delftstack4 delftstack5 delftstack6
正如我們所看到的,頭部是最小值。接下來,我們將演示如何從 Java 中的優先順序佇列中獲取最大值。
從 Java 中的優先順序佇列中獲取最大值
要從優先順序佇列中獲取最大值,我們應該首先按照降序對它們進行排序。要按降序對元素進行排序,我們可以使用比較器從 JAVA 中的優先順序佇列中獲取最大值。
例子:
package delftstack;
import java.util.*;
public class Priority_Queue {
public static void main(String args[]) {
// Initialize a priority queue with a custom comparator to sort the queue in descending order.
PriorityQueue<Integer> demo_priority_queue =
new PriorityQueue<Integer>(new Comparator<Integer>() {
public int compare(Integer left_hand_side, Integer right_hand_side) {
if (left_hand_side < right_hand_side)
return +1;
if (left_hand_side.equals(right_hand_side))
return 0;
return -1;
}
});
// add elements
demo_priority_queue.add(11);
demo_priority_queue.add(7);
demo_priority_queue.add(3);
demo_priority_queue.add(18);
demo_priority_queue.add(10);
demo_priority_queue.add(2);
demo_priority_queue.add(17);
demo_priority_queue.add(20);
demo_priority_queue.add(5);
// display the max PriorityQueue
System.out.println("The Priority Queue elements in max to min order:");
Integer val = null;
while ((val = demo_priority_queue.poll()) != null) {
System.out.print(val + " ");
}
}
}
上面的程式碼對優先順序佇列進行降序排序以獲得最大值。
輸出:
The Priority Queue elements in max to min order:
20 18 17 11 10 7 5 3 2
這裡有更多按降序對優先順序佇列進行排序以獲得最大值的方法。
例子:
package delftstack;
import java.util.*;
public class Priority_Queue {
public static void main(String args[]) {
// Initialize a priority queue with a custom comparator to sort the queue in descending order.
PriorityQueue<Integer> demo_priority_queue =
new PriorityQueue<Integer>(Collections.reverseOrder());
// PriorityQueue<Integer> demo_priority_queue = new PriorityQueue<Integer>((a,b) -> b - a);
// PriorityQueue<Integer> demo_priority_queue = new PriorityQueue<Integer>((a,b) ->
// b.compareTo(a)); add elements
demo_priority_queue.add(11);
demo_priority_queue.add(7);
demo_priority_queue.add(3);
demo_priority_queue.add(18);
demo_priority_queue.add(10);
demo_priority_queue.add(2);
demo_priority_queue.add(17);
demo_priority_queue.add(20);
demo_priority_queue.add(5);
// display the max PriorityQueue
System.out.println("The Priority Queue elements in max to min order:");
Integer val = null;
while ((val = demo_priority_queue.poll()) != null) {
System.out.print(val + " ");
}
}
}
Collections.reverseOrder() 是一個內建的比較器,用於按降序對優先順序佇列進行排序。註釋中的其他兩個比較器也執行相同的操作,我們可以使用它們中的任何一個。
輸出:
The Priority Queue elements in max to min order:
20 18 17 11 10 7 5 3 2
手動比較器和內建比較器的區別在於,我們還可以使用內建比較器對字串進行排序,並得到最大值,如下面的程式碼片段。
例子:
package delftstack;
import java.util.*;
public class Priority_Queue {
public static void main(String args[]) {
PriorityQueue<String> delftstack_queue = new PriorityQueue<String>(Collections.reverseOrder());
// Add the values to the priority queue
delftstack_queue.add("delftstack3");
delftstack_queue.add("delftstack2");
delftstack_queue.add("delftstack1");
delftstack_queue.add("delftstack4");
delftstack_queue.add("delftstack5");
delftstack_queue.add("delftstack6");
// head of the PriorityQueue
System.out.println(
"Head of the PriorityQueue, The maximum value: " + delftstack_queue.element());
// All Elements of the Priority Queue
System.out.println("\nAll PriorityQueue Elements:");
Iterator demo_iterator = delftstack_queue.iterator();
while (demo_iterator.hasNext()) {
System.out.print(demo_iterator.next() + " ");
}
}
}
輸出:
Head of the PriorityQueue, The maximum value: delftstack6
All PriorityQueue Elements:
delftstack6 delftstack4 delftstack5 delftstack2 delftstack3 delftstack1