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