开发者

Scala for-comprehension returning an ordered map

How can I use a for-comprehension that returns something I can assign to an ordered Map? This is a simplification of the code I have:

class Bar
class Foo(val name: String, val bar: Bar)
val myList: java.util.List[Foo] = ...
val result: ListMap[String, Bar] =
    for {
        foo <- myList
    } yield (foo.name, foo.bar)

I need to make sure my result is an ordered Map, in the order tuples are returned from the for-comprehension.

With the above, I get the error:

error: type mismatch;
found   : scala.collection.mutable.Buffer[(String,Bar)]
required: scala.collection.immutable.ListMap[String,Bar]
foo <- myList

This compiles:

class Bar
class Foo(val name: String, val bar: Bar)
val myList: java.util.List[Foo] = ...
val result: Predef.Map[String, Bar] =
    {
        for {
            foo <- myList
        } yield (foo.name, foo.bar)
    } toMap

but then I assum开发者_开发技巧e the map won't be ordered, and I need an explicit toMap call.

How can I achieve this?


The collection.breakOut is your good friend in such a case,

val result: collection.immutable.ListMap[String, Bar] = 
  myList.map{ foo => (foo.name, foo.bar) }(collection.breakOut)

If it is important to use for-comprehension expression, it will be done as follows,

val result: collection.immutable.ListMap[String, Bar] = {
  for { foo <- myList } yield (foo.name, foo.bar)
}.map(identity)(collection.breakOut)

Scala 2.8 breakOut has explained collection.breakOut very well.


You can achieve do it by using the companion object of ListMap class as followings:

class Bar
class Foo(val name: String, val bar: Bar)
val myList: java.util.List[Foo] = ...
val result = ListMap((for(foo <- myList) yield (foo.name, foo.bar)):_*)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜