在本节,我们将学习如何使用Scala的PriorityQueue来执行常用操作,例如初始化一个PriorityQueue指定元素的顺序,enqueue和dequeue元素并创建一个空的Queue。
关于PriorityQueue的更高级函数,例如:aggregate, fold, reduce, map, flatMap等将在 第8章集合函数 中讨论。
PriorityQueue是什么?
根据维基百科所说,PriorityQueue 类似于Queue的一种数据结构,但添加到队列中的元素与优先级有关,然后使用此优先级来确定哪些元素获得出队列或从队列中删除。
根据Scala文档所说,PriorityQueue 是使用 heap 数据结构实现的,并且只有方法 dequeue 和 dequeueAll 将按优先级顺序返回。
下面的代码展示了如何声明一个case class表示Donut对象:
println("Step 1: How to declare a case class to represent Donut object")
case class Donut(name: String, price: Double)
下面的代码展示了如何声明一个定义了Donut对象的PriorityQueue的顺序函数:
println("\nStep 2: How to declare a function which defines the ordering of a PriorityQueue of Donut objects")
def donutOrder(d: Donut) = d.price
NOTE:
- 如果你需要按最低价格排序,您可以**-d.price**
下面的代码展示了如何初始化Donut对象的PriorityQueue并指定元素的顺序:
println("\nStep 3: How to initialize a PriorityQueue of Donut objects and specify the Ordering")
import scala.collection.mutable.PriorityQueue
val priorityQueue1: PriorityQueue[Donut] = PriorityQueue(
Donut("Plain Donut", 1.50),
Donut("Strawberry Donut", 2.0),
Donut("Chocolate Donut", 2.50))(Ordering.by(donutOrder))
println(s"Elements of priorityQueue1 = $priorityQueue1")
当在 IntelliJ 中运行你的Scala应用程序时,你应该可以看到以下的输出:
Step 3: How to initialize a PriorityQueue of Donut objects and specify the Ordering
Elements of priorityQueue1 = PriorityQueue(Donut(Chocolate Donut,2.5), Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0))
下面的代码展示了如何使用enqueue函数添加一个函数到PriorityQueue:
println("\nStep 4: How to add an element to PriorityQueue using enqueue function")
priorityQueue1.enqueue(Donut("Vanilla Donut", 1.0))
println(s"Elements of priorityQueue1 after enqueue function is called = $priorityQueue1")
当在 IntelliJ 中运行你的Scala应用程序时,你应该可以看到以下的输出:
Step 4: How to add an element to PriorityQueue using enqueue function
Elements of priorityQueue1 after enqueue function is called = PriorityQueue(Donut(Chocolate Donut,2.5), Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0), Donut(Vanilla Donut,1.0))
下面的代码展示了如何使用 += 添加一个元素到PriorityQueue:
println("\nStep 5: How to add an element to PriorityQueue using +=")
priorityQueue1 += (Donut("Krispy Kreme Donut", 1.0))
println(s"Elements of priorityQueue1 after enqueue function is called = $priorityQueue1")
当在 IntelliJ 中运行你的Scala应用程序时,你应该可以看到以下的输出:
Step 5: How to add an element to PriorityQueue using +=
Elements of priorityQueue1 after enqueue function is called = PriorityQueue(Donut(Chocolate Donut,2.5), Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0), Donut(Vanilla Donut,1.0), Donut(Krispy Kreme Donut,1.0))
下面的代码展示了如何使用dequeue函数从PriorityQueue中移除一个元素:
println("\nStep 6: How to remove an element from PriorityQueue using the dequeue function")
val donutDequeued: Donut = priorityQueue1.dequeue()
println(s"Donut element dequeued = $donutDequeued")
println(s"Elements of priorityQueue1 after dequeued function is called = $priorityQueue1")
当在 IntelliJ 中运行你的Scala应用程序时,你应该可以看到以下的输出:
Step 6: How to remove an element from PriorityQueue using the dequeue function
Donut element dequeued = Donut(Chocolate Donut,2.5)
Elements of priorityQueue1 after dequeued function is called = PriorityQueue(Donut(Strawberry Donut,2.0), Donut(Plain Donut,1.5), Donut(Krispy Kreme Donut,1.0), Donut(Vanilla Donut,1.0))
NOTE:
- 队列中价格最高的donut元素是第一个被删除的元素。
下面的代码展示了如何初始化一个空的PriorityQueue:
println("\nStep 7: How to initialize an empty PriorityQueue")
val emptyPriorityQueue: PriorityQueue[String] = PriorityQueue.empty[String]
println(s"Empty emptyPriorityQueue = $emptyPriorityQueue")
当在 IntelliJ 中运行你的Scala应用程序时,你应该可以看到以下的输出:
Step 7: How to initialize an empty PriorityQueue
Empty emptyPriorityQueue = PriorityQueue()
我们的教程到此结束了,学习如何使用Scala的PriorityQueue,希望它对你有用!
在这一节,我们介绍了以下内容:
- 如何声明一个case class表示Donut对象
- 如何声明一个定义了Donut对象的PriorityQueue的顺序函数
- 如何初始化Donut对象的PriorityQueue并指定元素的顺序
- 如何使用enqueue函数添加一个函数到PriorityQueue
- 如何使用 += 添加一个元素到PriorityQueue
- 如何使用dequeue函数从PriorityQueue中移除一个元素
- 如何初始化一个空的PriorityQueue
- 不要忘记复习 第8章集合函数,我们将介绍aggregate, collect, fold, reduce, map, flatMap, scan, partition 等集合中暴露的丰富的函数
- Scala的PriorityQueue文档。
源代码可以在 Github - allaboutscala 找到。
下一节,我将展示如何使用Scala的Set。