开发者

How to print a Map in Scala

Okay, this question seems to be really stupid one, but my point is that if you take a look on Scala 2.7.6 API, they had made mappingToString method deprecated. Therefore, there should be more elegant alternative for printing custom-formatted Map. Since for nearly any purpose, having equivalence method of mkString i开发者_StackOverflow中文版n Map is really handy.

What do you guys think about it? What is your coding snippet for printing a Map except println?


mappingToString was specific to Map.

With the new collections framework in Scala2.8, a Map can be iterated by any IterableLike ,which extends TraversableLike.

The method mkstring (already there in 2.7 for Iterable) should be then used.

See this blog post "Strings" by Jesse, for 2.7 mkstring() examples:

/*
   Making use of raw strings to create a multi line string
   I add a | at the beginning of each line so that we can line up the quote nicely 
   in source code then later strip it from the string using stripMargin
*/
scala> val quote = """|I  don-t consider myself a pessimist.                                                                                                 
     |                |I think of a pessimist as someone who is waiting for it to rain.
     |                |And I feel soaked to the skin.
     | 
     |                |Leonard Cohen"""
quote: java.lang.String = 
|I don-t consider myself a pessimist. 
                      |I think of a pessimist as someone who is waiting for it to rain.
                      |And I feel soaked to the skin.

                      |Leonard Cohen

// capilize the first character of each line
scala> val capitalized = quote.lines.
     |                         map( _.trim.capitalize).mkString("\n")
capitalized: String = 
|I don-t consider myself a pessimist.
|I think of a pessimist as someone who is waiting for it to rain.
|And I feel soaked to the skin.

|Leonard Cohen

// remove the margin of each line
scala> quote.stripMargin        
res1: String = 
I don-t consider myself a pessimist. 
I think of a pessimist as someone who is waiting for it to rain.
And I feel soaked to the skin.

Leonard Cohen

// this is silly.  I reverse the order of each word but keep the words in order
scala> quote.stripMargin.         
     |       lines.               
     |       map( _.split(" ").   
     |              map(_.reverse).
     |              mkString (" ")).
     |      mkString("\n")
res16: String = 
I t-nod redisnoc flesym a .tsimissep
I kniht fo a tsimissep sa enoemos ohw si gnitiaw rof ti ot .niar
dnA I leef dekaos ot eht .niks

dranoeL nehoC


The mappingToString method was used to change how each pair of key/value was translated to String, which was then used by the toString method.

I think that's a lousy fit. It adds a mutability to an otherwise immutable data structure, for one thing. If you have specific printing requirements, then you are probably better off putting them in another class.


You could also combine Iterator.map() with mkString(), for example to create a query-string from a map[String,String]:

val queryString = updatedMap.map(pair => pair._1+"="+pair._2).mkString("?","&","")
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜