我们知道, 队列如下:F irst- 我正˚First-ØUT模型,但有时我们需要处理的基础上,优先级队列中的对象。 例如,假设我们有一个应用程序可以为日常交易生成股票报告,并处理大量数据并花费时间来处理它。 因此,客户正在将请求发送到实际上正在排队的应用程序,但是我们要先处理高级客户,然后再处理标准客户。 因此,在这种情况下,用Java实现PriorityQueue确实很有帮助。
PriorityQueue
类在Java 1.5中引入,并且是Java Collections Framework的一部分。 PriorityQueue是基于优先级堆的无界队列,并且默认情况下优先级队列的元素以自然顺序排序,或者我们可以在队列实例化时提供Comparator进行排序。
PriorityQueue不允许使用空值,并且我们无法创建不可比对象的PriorityQueue,例如我们拥有的任何自定义类。 我们使用java Comparable和Comparator接口对对象进行排序,而PriorityQueue使用它们对元素的优先级进行处理。
优先级队列的头是基于自然排序或基于比较器排序的最小元素,如果有多个具有相同排序的对象,则它可以随机轮询其中的任何一个。 当我们轮询队列时,它从队列中返回头对象。
PriorityQueue大小不受限制,但是我们可以在创建时指定初始容量。 当我们将元素添加到优先级队列时,它的容量会自动增长。
PriorityQueue 不是线程安全的 ,因此Java提供了PriorityBlockingQueue
类,该类实现了BlockingQueue接口以在Java多线程环境中使用。
PriorityQueue实现为入队和出队方法提供O(log(n))时间。 让我们来看一个自然排序以及Comparator的PriorityQueue示例。
我们有自定义类Customer
,它不提供任何类型的排序,因此,当我们尝试将其与PriorityQueue一起使用时,应为此提供一个比较器对象。
package com.journaldev.collections;public class Customer {private int id;private String name;public Customer(int i, String n){this.id=i;this.name=n;}public int getId() {return id;}public String getName() {return name;}}
我们将使用Java随机数生成来生成随机的客户对象。 对于自然排序,我将使用Integer,它也是一个Java包装器类 。
这是我们的最终测试代码,显示了如何使用PriorityQueue。
package com.journaldev.collections;import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;public class PriorityQueueExample {public static void main(String[] args) {//natural ordering example of priority queueQueue<Integer> integerPriorityQueue = new PriorityQueue<>(7);Random rand = new Random();for(int i=0;i<7;i++){integerPriorityQueue.add(new Integer(rand.nextInt(100)));}for(int i=0;i<7;i++){Integer in = integerPriorityQueue.poll();System.out.println("Processing Integer:"+in);}//PriorityQueue example with ComparatorQueue<Customer> customerPriorityQueue = new PriorityQueue<>(7, idComparator);addDataToQueue(customerPriorityQueue);pollDataFromQueue(customerPriorityQueue);}//Comparator anonymous class implementationpublic static Comparator<Customer> idComparator = new Comparator<Customer>(){@Overridepublic int compare(Customer c1, Customer c2) {return (int) (c1.getId() - c2.getId());}};//utility method to add random data to Queueprivate static void addDataToQueue(Queue<Customer> customerPriorityQueue) {Random rand = new Random();for(int i=0; i<7; i++){int id = rand.nextInt(100);customerPriorityQueue.add(new Customer(id, "Pankaj "+id));}}//utility method to poll data from queueprivate static void pollDataFromQueue(Queue<Customer> customerPriorityQueue) {while(true){Customer cust = customerPriorityQueue.poll();if(cust == null) break;System.out.println("Processing Customer with ID="+cust.getId());}}}
请注意,我正在使用java匿名类来实现Comparator接口并创建基于id的比较器。
当我在测试程序上运行时,得到以下输出:
Processing Integer:9
Processing Integer:16
Processing Integer:18
Processing Integer:25
Processing Integer:33
Processing Integer:75
Processing Integer:77
Processing Customer with ID=6
Processing Customer with ID=20
Processing Customer with ID=24
Processing Customer with ID=28
Processing Customer with ID=29
Processing Customer with ID=82
Processing Customer with ID=96
从输出中可以明显看出,最少的元素在首位,并且被首先轮询。 如果在创建customerPriorityQueue
时不提供比较器,它将在运行时引发ClassCastException。
Exception in thread "main" java.lang.ClassCastException: com.journaldev.collections.Customer cannot be cast to java.lang.Comparableat java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:633)at java.util.PriorityQueue.siftUp(PriorityQueue.java:629)at java.util.PriorityQueue.offer(PriorityQueue.java:329)at java.util.PriorityQueue.add(PriorityQueue.java:306)at com.journaldev.collections.PriorityQueueExample.addDataToQueue(PriorityQueueExample.java:45)at com.journaldev.collections.PriorityQueueExample.main(PriorityQueueExample.java:25)
翻译自: https://www.javacodegeeks.com/2013/07/java-priority-queue-priorityqueue-example.html