Scala:有没有一种方法可以像在Java中那样使用PriorityQueue? [英] Scala: Is there a way to use PriorityQueue like I would in Java?

查看:288
本文介绍了Scala:有没有一种方法可以像在Java中那样使用PriorityQueue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在scala.collection.mutable.PriorityQueue中使用的类,但我不想为此而将其设置为Ordered [A]。我不认为我想对PriorityQueue使用的排序是该类的自然排序。

I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but I don't want to make it Ordered[A] just for this one purpose. I don't consider the ordering I want to use with respect to the PriorityQueue as the natural ordering of the class.

class MyObject (sequence: Int, values: List[String]) ...

因此,在我的PriorityQueue中,我希望这些值按序列排序。但是,仅仅因为两个对象具有相同的顺序,并不会使它们自然地相等,因为它们的值的内容可能不同。

So, in my PriorityQueue, I would like the values to be ordered by 'sequence'. However, just because two objects have the same sequence doesn't make them naturally equal since the contents of their 'values' may be different.

在Java中,很高兴能够为PriorityQueue提供备用Comparator对象。我的比较器只会根据对象的顺序对它们进行排序,而忽略它们的值。

This is where, in Java, it's nice to be able to supply an alternate Comparator object to the PriorityQueue. My Comparator would simply order the objects with respect to their 'sequence' and ignores their 'values'.

必须使用 A<%Ordered [A]参数化PriorityQueue类。

The PriorityQueue class must be parameterized with a "A <% Ordered[A]"

class PriorityQueue[A <% Ordered[A]] extends ... 

根据我的阅读,这意味着我的课程必须扩展Ordered [A]或我必须提供对Ordered [A]的隐式def类型转换,老实说,这感觉不雅。

From what I've read, this means my class must extend Ordered[A] or I must provide an "implicit def" type conversion to Ordered[A], which, honestly, feels inelegant.

Java解决方案似乎更具功能性,允许我传递类似于Comparator的对象,而不是强迫我进入类层次结构或猴子修补类。

The Java solution seems more "functional" allowing me to pass a Comparator function-like object instead of forcing me into a class hierarchy or monkeypatching my class.

我意识到使用PrioirityQueue可以有很多选择,但是我觉得我可能在这里违背了Scala的学习曲线,并且不想放弃而不完全探索这个设计决策。

I realize there are alternatives to using PrioirityQueue, but I feel like I may be bumping up against the Scala learning curve here and don't want to give up without exploring this design decision fully.

这只是Scala库中一个不幸的决定,还是我误解了某种使PriorityQueue更加实用和实用的调用约定?

Is this just an unfortunate decision in the Scala library or am I misunderstanding some sort of calling convention that makes PriorityQueue more usable and 'functional'?

谢谢

推荐答案

语法

class PriorityQueue[A <% Ordered[A]] ...

实际上只是在上面加了一点糖

is really just a light sugaring on top of

class PriorityQueue[A]()(implicit convert: A => Ordered[A]) ...

这意味着您可以编写自己的方法A => Ordered [ A]

This means you can write your own method A => Ordered[A]

case class Foo(n: Int)
def orderedFoo(f: Foo): Ordered[Foo] = new Ordered[Foo] {
  def compare(other: Foo) = f.n.compare(other.n)
}

并手动将其传递到您的PriorityQueue构造函数中

And manually pass it into your PriorityQueue constructor

new PriorityQueue[Foo]()(orderedFoo)

这篇关于Scala:有没有一种方法可以像在Java中那样使用PriorityQueue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆