java 优先队列
Every now and then we need to process items of a queue in a particular order. Priority queue is a Data Structure that does the job. Java priority queue is different from “normal” queue. Instead of “First-In-First-Out”, it retrieves the items in order of their priority.
我们有时需要按特定顺序处理队列中的项目。 优先级队列是完成任务的数据结构。 Java优先级队列与“普通” 队列不同 。 代替“先进先出”,它按优先级顺序检索项目。
The java.util.PriorityQueue
class, provides us an implementation of such a data type, by using priority heap implementation internally. Java PriorityQueue is an unbounded queue. It was introduced in Java 1.5 and enhanced in Java SE 8 release. PriorityQueue is internally implemented by following “Priority Heap” data structure.
java.util.PriorityQueue
类通过内部使用优先级堆实现为我们提供了这种数据类型的实现。 Java PriorityQueue是一个无界队列。 它是在Java 1.5中引入的,并在Java SE 8版本中得到了增强。 PriorityQueue通过遵循“ Priority Heap”数据结构在内部实现。
Here is the PriorityQueue class hierarchy:
这是PriorityQueue类层次结构:
PriorityQueue Class Diagram:
PriorityQueue类图:
Priority Queue elements are ordered by their natural ordering unless we provide a Comparator
while creating it.
The elements are ordered in ascending order by default, hence the head of the queue is the element whose priority is lowest.
除非我们在创建Comparator
时提供它,否则Priority Queue元素将按照其自然顺序进行排序。
默认情况下,元素按升序排列,因此队列的开头是优先级最低的元素。
If there are two elements which are eligible to become the head at the same time, this kind of ties are broken arbitrarily.
如果有两个元素可以同时成为负责人,则这种联系会被任意打破。
Let’s create a PriorityQueue
, containing various tasks:
让我们创建一个PriorityQueue
,其中包含各种任务:
PriorityQueue tasks=new PriorityQueue();
tasks.add("task1");
tasks.add("task4");
tasks.add("task3");
tasks.add("task2");
tasks.add("task5");
This creates a PriorityQueue of tasks, which will be ordered by the natural ordering of String
.
Let’s create another PriorityQueue which orders the tasks in reverse order of natural ordering. So we need to pass a Comparator:
这将创建一个PriorityQueue任务,该任务将按String
的自然顺序进行排序。
让我们创建另一个PriorityQueue,它以自然顺序的相反顺序对任务进行排序。 因此,我们需要通过一个比较器:
PriorityQueue reverseTasks=new PriorityQueue(Comparator.reverseOrder());
reverseTasks.add("task1");
reverseTasks.add("task4");
reverseTasks.add("task3");
reverseTasks.add("task2");
reverseTasks.add("task5");
Now, let’s take a look at all the methods available for PriorityQueue and use them:
现在,让我们看一下PriorityQueue可用的所有方法并使用它们:
System.out.println(tasks.comparator());
System.out.println(reverseTasks.comparator());
The output will be:
比较器比较器() -此方法返回用于对队列中的元素进行排序的比较器 。 如果未指定比较器,并且队列根据其元素的自然顺序排序,则返回null。System.out.println(tasks.comparator());
System.out.println(reverseTasks.comparator());
输出将是:
System.out.println(tasks.contains("task3"));
This prints:
boolean contains(Object o) –如果队列包含指定的元素,则返回true。System.out.println(tasks.contains("task3"));
打印:
System.out.println(tasks.peek());
System.out.println(reverseTasks.peek());
Gives us:
E peek() –检索此队列的开头,如果此队列为空,则返回null。 换句话说,它返回具有最高优先级的元素。System.out.println(tasks.peek());
System.out.println(reverseTasks.peek());
给我们:
System.out.println(“Poll on tasks: ”+tasks.poll());
System.out.println(“Poll on reverseTasks: ”+reverseTasks.poll());
And then peek:
We’ll have the following outptut:
Poll on tasks: task1
Poll on reverseTasks: task5
Peek on tasks: task2
Peek on reverseTasks: task4
E poll() –此方法还检索队列的头部(具有最高优先级的元素),如果队列为空,则返回null。 但是与peek()不同,它还删除了元素。 然后偷看:
System.out.println(“Peek on tasks: ”+tasks.peek());
System.out.println(“Peek on reverseTasks: ”+reverseTasks.peek());
我们将提供以下输出:
Apart from these, the PriorityQueue
also inherits the methods from the Collection
and Object
classes.
除此之外, PriorityQueue
还继承了Collection
和Object
类的方法。
This implementation of priority queue is not thread-safe. So, if we need synchronised access, we need to use PriorityBlockingQueue.
优先级队列的此实现不是线程安全的。 因此,如果需要同步访问,则需要使用PriorityBlockingQueue 。
Reference: API Doc
参考: API文档
翻译自: https://www.journaldev.com/16254/priority-queue-java
java 优先队列