Java中的逆序优先队列
当应该根据优先级处理对象时,将使用 PriorityQueue。众所周知,队列遵循先进先出算法,但有时需要根据优先级处理队列中的元素,这就是PriorityQueue发挥作用的时候。 PriorityQueue 基于优先级堆。优先级队列的元素按照自然顺序排序,或者由队列构建时提供的 Comparator 排序,具体取决于使用的构造函数。
宣言:
public class PriorityQueue extends AbstractQueue implements Serializable
其中E是此队列中保存的元素类型
PriorityQueue 的类型
- 最大优先级队列
- 最小优先级队列
默认优先级队列示例
Java
// Java program to demonstrate the
// working of default PriorityQueue
import java.util.*;
class PriorityQueueDemo {
// Main Method
public static void main(String args[])
{
// Creating empty priority queue
PriorityQueue pQueue
= new PriorityQueue();
// Adding items to the pQueue using add()
pQueue.add(10);
pQueue.add(20);
pQueue.add(15);
pQueue.add(5);
// Printing the top element of PriorityQueue
System.out.println(pQueue.peek());
// Printing the top element and removing it
// from the PriorityQueue container
System.out.println(pQueue.poll());
// Printing the top element again
System.out.println(pQueue.peek());
}
}
Java
// Java program to demonstrate the
// working of PriorityQueue in reverse order
import java.util.*;
class PriorityQueueDemo {
// Main Method
public static void main(String args[])
{
// Creating empty priority queue
PriorityQueue pQueue
= new PriorityQueue(
Collections.reverseOrder());
// Adding items to the pQueue using add()
pQueue.add(10);
pQueue.add(20);
pQueue.add(15);
pQueue.add(5);
// Printing the top element of PriorityQueue
System.out.println(pQueue.peek());
// Printing the top element and removing it
// from the PriorityQueue container
System.out.println(pQueue.poll());
// Printing the top element again
System.out.println(pQueue.peek());
}
}
Java
// Java program to demonstrate the
// working of PriorityQueue in reverse order
import java.util.*;
public class PriorityQueueDemo {
// Main Method
public static void main(String[] args)
{
// Creating empty priority queue
// with custom Comparator
PriorityQueue pQueue
= new PriorityQueue(
new Comparator() {
// Compare method for place element in
// reverse order
public int compare(Integer a, Integer b)
{
if (a < b)
return 1;
if (a > b)
return -1;
return 0;
}
});
// Adding items to the pQueue using add()
pQueue.add(10);
pQueue.add(15);
pQueue.add(20);
pQueue.add(5);
// Printing the top element of PriorityQueue
System.out.println(pQueue.peek());
// Printing the top element and removing it
// from the PriorityQueue container
System.out.println(pQueue.poll());
// Printing the top element again
System.out.println(pQueue.peek());
}
}
Java
// Java program to demonstrate the
// working of PriorityQueue in reverse order
import java.util.*;
class PriorityQueueDemo {
// Main Method
public static void main(String args[])
{
// Creating empty priority queue
PriorityQueue pQueue
= new PriorityQueue((a, b) -> b - a);
// Adding items to the pQueue using add()
pQueue.add(10);
pQueue.add(20);
pQueue.add(15);
pQueue.add(5);
// Printing the top element of PriorityQueue
System.out.println(pQueue.peek());
// Printing the top element and removing it
// from the PriorityQueue container
System.out.println(pQueue.poll());
// Printing the top element again
System.out.println(pQueue.peek());
}
}
5
5
10
在Java中,Priority Queue默认实现了min Priority Queue,如果我们需要将Priority Queue的顺序从min改为max Priority Queue,那么我们使用如下一些方法:
- 使用默认的 Comparator Collections.reverseOrder()
- 使用自定义比较器
- 使用lambda 表达式
方法 1:使用默认的 Comparator Collections.reverseOrder()
Collections.reverseOrder()方法用于获取默认比较器的反向行为。这是默认的比较器 Java.util 包。
例子:
Java
// Java program to demonstrate the
// working of PriorityQueue in reverse order
import java.util.*;
class PriorityQueueDemo {
// Main Method
public static void main(String args[])
{
// Creating empty priority queue
PriorityQueue pQueue
= new PriorityQueue(
Collections.reverseOrder());
// Adding items to the pQueue using add()
pQueue.add(10);
pQueue.add(20);
pQueue.add(15);
pQueue.add(5);
// Printing the top element of PriorityQueue
System.out.println(pQueue.peek());
// Printing the top element and removing it
// from the PriorityQueue container
System.out.println(pQueue.poll());
// Printing the top element again
System.out.println(pQueue.peek());
}
}
20
20
15
方法 2:使用自定义比较器
这 Java.util.PriorityQueue.comparator()方法共享一个重要的函数,即设置和返回可用于对 PriorityQueue 中的元素进行排序的比较器。如果队列遵循元素的自然排序模式,则该方法返回空值。
例子:
Java
// Java program to demonstrate the
// working of PriorityQueue in reverse order
import java.util.*;
public class PriorityQueueDemo {
// Main Method
public static void main(String[] args)
{
// Creating empty priority queue
// with custom Comparator
PriorityQueue pQueue
= new PriorityQueue(
new Comparator() {
// Compare method for place element in
// reverse order
public int compare(Integer a, Integer b)
{
if (a < b)
return 1;
if (a > b)
return -1;
return 0;
}
});
// Adding items to the pQueue using add()
pQueue.add(10);
pQueue.add(15);
pQueue.add(20);
pQueue.add(5);
// Printing the top element of PriorityQueue
System.out.println(pQueue.peek());
// Printing the top element and removing it
// from the PriorityQueue container
System.out.println(pQueue.poll());
// Printing the top element again
System.out.println(pQueue.peek());
}
}
20
20
15
方法 3:使用lambda 表达式
自 Java 8 开始使用Lambda 表达式, lambda函数将其输入参数命名为 a 和 b 并返回 (ba),这与 int 比较器类所做的基本相同,只是它返回 ab。
例子:
Java
// Java program to demonstrate the
// working of PriorityQueue in reverse order
import java.util.*;
class PriorityQueueDemo {
// Main Method
public static void main(String args[])
{
// Creating empty priority queue
PriorityQueue pQueue
= new PriorityQueue((a, b) -> b - a);
// Adding items to the pQueue using add()
pQueue.add(10);
pQueue.add(20);
pQueue.add(15);
pQueue.add(5);
// Printing the top element of PriorityQueue
System.out.println(pQueue.peek());
// Printing the top element and removing it
// from the PriorityQueue container
System.out.println(pQueue.poll());
// Printing the top element again
System.out.println(pQueue.peek());
}
}
20
20
15