-
java 优先队列_优先队列Java
2020-07-14 02:34:52java 优先队列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....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优先级队列与“普通” 队列不同 。 代替“先进先出”,它按优先级顺序检索项目。
优先队列Java (Priority Queue 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类图:
Java PriorityQueue构造函数 (Java PriorityQueue Constructors)
- PriorityQueue() – Creates a PriorityQueue with the default initial capacity, i.e. 11 PriorityQueue() –使用默认初始容量(即11 )创建一个PriorityQueue
- PriorityQueue(Collection c) – Creates a PriorityQueue with the elements in the specified collection PriorityQueue(Collection c) –使用指定集合中的元素创建一个PriorityQueue
- PriorityQueue(int initialCapacity) – Creates a PriorityQueue with the specified initial capacity PriorityQueue(int initialCapacity) –创建具有指定初始容量的PriorityQueue
- PriorityQueue(int initialCapacity, Comparator comparator) – Creates a PriorityQueue with the provided initial capacity and the ordering of its elements is according to the specified comparator PriorityQueue(int initialCapacity,Comparator比较器) –创建具有提供的初始容量的PriorityQueue,其元素的顺序根据指定的比较器
- PriorityQueue(PriorityQueue c) – Creates a PriorityQueue containing the elements in the specified priority queue PriorityQueue(PriorityQueue c) –创建一个包含指定优先级队列中元素的PriorityQueue
- PriorityQueue(SortedSet c) – Creates a PriorityQueue containing the elements in the specified sorted set PriorityQueue(SortedSet c) –创建一个包含指定排序集中的元素的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.
如果有两个元素可以同时成为负责人,则这种联系会被任意打破。
Java PriorityQueue示例 (Java PriorityQueue Example)
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");
Java PriorityQueue方法 (Java PriorityQueue Methods)
Now, let’s take a look at all the methods available for PriorityQueue and use them:
现在,让我们看一下PriorityQueue可用的所有方法并使用它们:
- Boolean add(E e) – This method inserts the specified element in the queue.
We have already added 5 tasks in our queue using this method. 布尔值add(E e) –此方法将指定的元素插入队列。
我们已经使用这种方法在队列中添加了5个任务。 - Comparator comparator() – This method returns the Comparator used to order the elements in this queue. It returns null if no comparator was specified and the queue is sorted according to the natural ordering of its elements.
So, if we do: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());
输出将是:
- boolean contains(Object o) – Returns true if the queue contains the specified element.
Let’s check if “task3” belongs to the Priority queue tasks:System.out.println(tasks.contains("task3"));
This prints:
boolean contains(Object o) –如果队列包含指定的元素,则返回true。
让我们检查“ task3”是否属于优先级队列任务:System.out.println(tasks.contains("task3"));
打印:
- boolean offer(E e) – Just like the add() method, this method also adds an element to the queue.
The offer() and add() methods are actually a bit different for capacity constrained queues, but in case of PriorityQueue, both are same. Unlike add(), offer() does not throw an exception even if it fails to add the element in the queue. boolean offer(E e) –就像add()方法一样,此方法也将元素添加到队列中。
对于容量受限制的队列,offer()和add()方法实际上有些不同,但是对于PriorityQueue,两者是相同的。 与add()不同,即使offer()无法将元素添加到队列中,它也不会引发异常。 - E peek() – Retrieves the head of this queue, or returns null if this queue is empty. In other words, it returns the element with highest priority.
So the following code: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());
给我们:
- E poll() – This method also retrieves the head of the queue(element with highest priority), or returns null if the queue is empty. But unlike peek(), it also removes the element.
So, if we call poll():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:
E poll() –此方法还检索队列的头部(具有最高优先级的元素),如果队列为空,则返回null。 但是与peek()不同,它还删除了元素。Poll on tasks: task1 Poll on reverseTasks: task5 Peek on tasks: task2 Peek on reverseTasks: task4
因此,如果我们调用poll():然后偷看:
System.out.println(“Peek on tasks: ”+tasks.peek()); System.out.println(“Peek on reverseTasks: ”+reverseTasks.peek());
我们将提供以下输出:
- int size() – Returns the number of elements in the queue. int size() –返回队列中元素的数量。
- boolean remove(Object o) – Removes the specified element from the queue, if it’s present. If two same elements are present, it only removes one of them. boolean remove(Object o) -从队列中删除指定的元素(如果存在)。 如果存在两个相同的元素,则仅删除其中之一。
- Object[] toArray() – Returns an array containing all the elements in the queue. Object [] toArray() –返回包含队列中所有元素的数组。
- T[] toArray(T[] a) – Returns an array containing all the elements in the queue, and the type of the returned array is that of the specified array. T [] toArray(T [] a) –返回包含队列中所有元素的数组,并且返回的数组的类型为指定数组的类型。
- Iterator iterator() – Returns an iterator for the queue. Iterator iterator() –返回队列的迭代器。
- void clear() – Removes all of the elements from the queue. void clear() –从队列中删除所有元素。
Apart from these, the
PriorityQueue
also inherits the methods from theCollection
andObject
classes.除此之外,
PriorityQueue
还继承了Collection
和Object
类的方法。Java PriorityQueue时间复杂度 (Java PriorityQueue Time Complexity)
- For enqueing and dequeing methods, the time complexity is O(log(n)) 对于入队和出队方法,时间复杂度为O(log(n))
- For the remove(Object) and contains(Object) methods, the time complexity is linear 对于remove(Object)和contains(Object)方法,时间复杂度是线性的
- For the retrieval methods, it has constant time complexity 对于检索方法,它具有恒定的时间复杂度
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文档
java 优先队列
-
优先队列java-PriorityQueue
2017-08-29 16:04:34PriorityQueue,也就是优先队列,所谓的优先队列,就是在队列中根据某一个特征值自动进行排序,优先队列分为两种,最大优先队列和最小优先队列,优先队列的一个最大特性就是,当插入元素或者删除元素的时候,队列会...PriorityQueue,也就是优先队列,所谓的优先队列,就是在队列中根据某一个特征值自动进行排序,优先队列分为两种,最大优先队列和最小优先队列,优先队列的一个最大特性就是,当插入元素或者删除元素的时候,队列会自动进行调整,保证队首元素一定是优先权最大/最小。正是由于优先队列的这种特性,优先队列可以被用在很多地方,比如作业调度,进程调度等。
堆与堆排序
优先队列的内部是采用堆这一种数据结构来实现的,并且,在进行操行的时候,时刻进行堆的维护。
比较常用的一种堆结构是二叉堆,二叉堆的实质是一个二叉树,只不过这个二叉树有点特殊,每一个父子节点都是其子孙节点中的最大值,称之为最大堆,反之则称之为最小堆,下面采用最大堆来演示,最小堆原理相同,二叉堆一般采用数组来进行维护即可,并且,由于其本身的特性,可以得出结论,堆的左孩子索引为 i * 2+1, 右孩子索引为 i * 2 + 2,其中i为父亲节点的索引
从图中可以看到,每一个父子节点的值都比其子孙节点大,也就是说,根节点的值是整个堆中的值最大的一个,这就是最大
堆名字的由来了建立二叉堆的方法非常简单,根据二叉堆的特性,只需要对每个元素执行调整即可,调整的具体过程为,从后往前调整,如果发现一个元素的值比其孩子节点(左右孩子)的值小,则跟其进行交换,并且递归其发生交换的孩子,继续进行判断,交换,直至到达第一个元素,不过这里有一个可以提高效率的地方,从元素个数/2-1开始即可
二叉堆排序是根据堆的这种特性而出现的一种排序算法,算法的流程为,首先建立二叉堆,然后每次交换第一个元素(也就是当前的最大值)与最后一个元素,调整堆,将堆的大小-1,重复上面的操作,直至堆大小为1,具体代码如下:
优先队列(PriorityQueue)源码剖析
首先是构造方法
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
堆的建立与调整
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
插入元素
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
获得元素的索引
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
删除元素
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
优先队列的本质其实就是一个最小堆,如果要使用最大优先队列,则需要传入自定义的比较器,在优先队列中,不需要存在空元素,并且要求传入优先队列中的元素必须是实现了Comparable接口
-
优先队列JAVA与C++区别
2019-11-27 14:18:03JAVA优先队列 package com.bbs.zebro.service; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; public class PriorityQueueTester { public static void main(String[] ...JAVA优先队列
package com.bbs.zebro.service; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; public class PriorityQueueTester { public static void main(String[] args){ Queue<Integer> pq = new PriorityQueue<Integer>(11); pq.add(1); pq.add(2); pq.add(3); pq.add(4); pq.add(5); pq.add(6); System.out.println(pq.poll().toString());//默认是小根堆 输出1 Comparator<Integer> order = new Comparator<Integer>(){ public int compare(Integer o1, Integer o2) { // TODO Auto-generated method stub if(o1 < o2) { return 1; } else if(o1 > o2) { return -1; } else { return 0; } } }; Queue<Integer> pq2 = new PriorityQueue<Integer>(11,order); pq2.add(1); pq2.add(2); pq2.add(3); pq2.add(4); pq2.add(5); System.out.println(pq2.poll().toString());//这么定义是大根堆 输出5 } }
C++
#include<iostream> #include<string> #include<vector> #include<queue> #include<stdlib.h> using namespace std; int main(int argc,char** argv){ priority_queue<int> pq;//等价于 priority_queue<int,vector<int>,less<int> > pq; pq.push(1); pq.push(2); pq.push(3); pq.push(4); pq.push(5); cout<<pq.top()<<endl;//默认是大根堆 输出5 priority_queue<int,vector<int>,greater<int> > pq2; pq2.push(1); pq2.push(2); pq2.push(3); pq2.push(4); pq2.push(5); cout<<pq2.top()<<endl;//此时是小根堆 输出1 }
但是二者对于<号如果重载相同的意义,他们的表现是相同的.例如如果<都是表示本对象小于另外一个对象的比较结果,那么就是大根堆,否则就是小根堆.
使用结构体时注意在结构体里面重载相应的比较函数~~~. -
优先队列 java PriorityQueue
2015-06-21 14:31:05PriorityQueue 优先队列 效率比较快 采用Heap排序算法/*
PriorityQueue中主要用到的就是Heap排序
*/
import java.util.PriorityQueue;
public class Main {
//队首为最大
static class Dog implements Comparable<Dog>{
private int num;
public Dog(int num){
this.num=num;
}
public int getNum(){
return this.num;
}
@Override
public int compareTo(Dog o) {
// TODO Auto-generated method stub
if(o.getNum()<=this.num)
return -1;
return 1;
}
}
//队首为最小
static class Cat implements Comparable<Cat>{
private int num;
public Cat(int num) {
// TODO Auto-generated constructor stub
this.num=num;
}
public int getNum(){
return this.num;
}
@Override
public int compareTo(Cat o) {
// TODO Auto-generated method stub
if(o.getNum()<=this.num)
return 1;
return -1;
}
}
public static void main(String[] args){
PriorityQueue<Dog> p0=new PriorityQueue<Dog>();
PriorityQueue<Cat> p1=new PriorityQueue<Cat>();
p1.offer(6);
p1.offer(0);
p1.offer(8);
p1.offer(7);
p1.offer(-2);p0.offer(6);
p0.offer(0);
p0.offer(8);
p0.offer(7);
p0.offer(-2);System.out.println(p0.poll());
System.out.println(p1.poll());
}
} -
利用二叉堆实现优先队列Java实现
2019-02-23 14:37:34* 利用二叉堆实现优先队列 * @author Prince * * @param <Key> */ public class MaxPQ<Key extends Comparable<Key>> { private Key[] pq; //基于堆的完全二叉树 ... -
《算法导论》第6章优先队列Java代码实现
2019-12-13 19:55:32最大优先队列类为: //基于堆结构的最大值优先的优先队列 public class MaxPrimaryQueue<Key extends Comparable<Key>>{ //用于保存堆的元素 private Key[] pq; //用于保存堆中目前存在的元素个数 ... -
基于二叉堆的优先队列java实现
2018-05-02 20:58:03堆有序的·二叉树,就是每个节点值都是大于等于它的子节点值。 这里用大小为N+1的数组pq来表示一个大小为N的堆,不使用pq[0]。 关于二叉树,有一个特点很重要,就是位置k的父节点是k/2,子节点是2*k和2*k+1。... -
HeapSort and PriorityQueue(堆排序算法和优先队列Java代码实现)
2019-03-22 11:40:15//优先队列 public class PriorityQueue extends HeapSort{ public static void main(String[] args) { int arr[] = { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 }; PriorityQueue p1 = new PriorityQueue(); System.... -
数据结构-优先队列(最大优先队列、最小优先队列、索引优先队列)Java实现
2020-08-09 20:28:09数据结构-优先队列(最大优先队列、最小优先队列、索引优先队列) 由于队列先进先出的特性,无法灵活按照优先级进行数据的获取。但是在应用中又有此类需求,例如计算机按照优先级进行的任务调度。 所以需要对于队列... -
Java优先队列
2020-09-25 15:55:35Java优先队列 Java优先队列默认为最小堆 PriorityQueue<Integer> p = new PriorityQueue<Integer>(); for(int i = 1; i <= 10; i ++) p.add(i); System.out.println(p.poll()); 运行结果为1 重写... -
java优先队列_Java算法之优先队列
2021-03-03 23:58:24优先队列指的是,一遍收集数据,一遍给数据排序,不管数据的收集是否还在进行,最终得到一个完全二叉树,根节点是最小元素,算法将会把最小元素永远放到树顶,每次也只会取根节点的元素,也就是每取一次数据,若数据... -
java 优先队列_Java的优先队列PriorityQueue原理及实例分析
2021-02-27 08:09:47这篇文章主要介绍了Java的优先队列PriorityQueue原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下一、优先队列概述优先队列PriorityQueue是Queue接口的... -
Java 优先队列PriorityQueue
2020-04-21 12:43:07Java中没有一个叫做Heap的类,确有一个可以实现和堆一样功能的类PriorityQueue,即优先队列。 从名字可以看出,它其实是一个队列,这个队列和其他队列一样有一个入口,一个出口,不一样的地方就是,每个进入队列的... -
java 优先队列 用法_JAVA中优先队列的使用
2021-03-09 05:59:25优先队列一.优先队列概述先队列PriorityQueue是Queue接口的实现,可以对其中元素进行排序,可以放基本数据类型的包装类(如:Integer,Long等)或自定义的类对于基本数据类型的包装器类,优先队列中元素默认排列顺序是... -
java优先队列
2020-03-16 16:28:15title: java优先队列 date: 2018-12-16 15:54:20 updated: 2020-03-07 13:39:312020-03-07 13:42:15 categories: java tags: - java 此文档为java优先队列的学习总结 java中优先队列和堆的详细使用 来源:java中... -
java实现优先队列_Java优先队列(PriorityQueue)实现重写compare操作
2021-03-04 04:10:38Java优先队列(PriorityQueue)实现重写compare操作发布时间:2020-10-29 15:15:11来源:亿速云阅读:82作者:Leah这篇文章将为大家详细讲解有关Java优先队列(PriorityQueue)实现重写compare操作,文章内容质量较高,... -
java实现优先队列_Java优先队列的简单实现
2021-03-04 04:09:55importjava.util.ArrayList;class MyHeap>{private ArrayListdata;private intMaxSize;private intsize;publicMyHeap() {this.MaxSize=0;this.size=0;}public booleanadd(Type Elem) {if(this.size>=this.MaxS... -
java 优先队列 用法_关于java优先队列的用法
2021-02-28 15:53:26java提供PriorityQueue类实现优先队列,但是处理时默认是以数字大小(字符串是以字典顺序)进行设置优先方式。如果自己使用的话,一般可以将插入元素封装成类,类包含元素和int型变量来设置优先级。例如代码如下:... -
java 优先队列 用法_java优先队列PriorityQueue中Comparator的用法详解
2021-02-28 15:52:50在使用java的优先队列PriorityQueue的时候,会看到这样的用法。PriorityQueue queue = new PriorityQueue(new Comparator(){@Overridepublic int compare(Integer o1, Integer o2){return o1.compareTo(o2);}});那... -
java 优先队列实现_Java基于堆结构实现优先队列功能示例
2021-02-12 20:15:34本文实例讲述了Java基于堆结构实现优先队列功能。分享给大家供大家参考,具体如下:package Demo;import java.util.NoSuchElementException;/** 小顶堆 java使用堆结构实现优先队列*/public class JPriorityQueue {@... -
Java 优先队列PriorityQueue的最基础使用
2021-03-13 21:04:35Java 优先队列PriorityQueue的最基础使用树、堆和优先队列Java中使用PriorityQueue实现优先队列使用Comparator将优先队列改为最大堆优先队列的简单应用例子一:查找一个数组中第K小的数例子一:查找一个数组中第K小... -
java 线程池 优先队列_Java 优先队列 PriorityQueue PriorityBlockingQueue 源码分析
2021-03-08 07:13:40和PriorityQueue的区别:增加了 重入锁ReentrantLock Condition,用于队列空情况下的阻塞 allocationSpinLock,通过CAS手段对queue扩容 private void tryGrow(Object[] array, int oldCap) { lock.unlock();... -
[java]优先队列
2013-08-22 18:23:54[java]优先队列 Java util包中的PriorityQueue类用来表示优先队列。优先队列是一个以集合为基础的抽象数据类型,队列中的每个元素都有一个优先级值。优先级值用来表示该元素的出列的优先级。 Java中的优先... -
java 优先队列
2018-11-16 20:13:53队列的基本准则就是先进先出,优先队列有两种,一种就是最大优先队列,就是不管入队元素,先让队列里的最大值先出队,另一种就是最小优先队列,就是不管入队元素,先让队列里的最小的元素先出队,运用二叉堆的方法... -
java优先队列_java中(优先队列)PriorityQueue的使用
2021-03-05 15:32:40import java.util.*;public class test1 {public static void PrintPr(Queue> queue){while(queue.peek()!=null){System.out.print(queue.remove()+" ");}System.out.println();}public static void main(String...