如何在 Scala 中打印地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1611688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print a Map in Scala
提问by Ekkmanz
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 in Map is really handy.
好的,这个问题似乎非常愚蠢,但我的观点是,如果您查看 Scala 2.7.6 API,他们已经弃用了 mappingToString 方法。因此,应该有更优雅的选择来打印自定义格式的地图。由于几乎任何目的,在 Map 中使用 mkString 的等效方法真的很方便。
What do you guys think about it? What is your coding snippet for printing a Map except println?
大家怎么看?除了 println 之外,您用于打印 Map 的编码片段是什么?
采纳答案by Daniel C. Sobral
The mappingToStringmethod was used to change how each pair of key/value was translated to String, which was then used by the toStringmethod.
该mappingToString方法用于更改将每对键/值转换为 String 的方式,然后由该toString方法使用。
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.
我认为这很糟糕。一方面,它为原本不可变的数据结构增加了可变性。如果您有特定的打印要求,那么最好将它们放在另一个类中。
回答by VonC
mappingToStringwas specific to Map.
mappingToString特定于Map.
With the new collections framework in Scala2.8, a Mapcan be iterated by any IterableLike,which extends TraversableLike.
使用 Scala2.8 中的新集合框架, aMap可以被 any 迭代IterableLike,它扩展了TraversableLike。
The method mkstring(already there in 2.7 for Iterable) should be then used.
然后应该使用mkstring方法(在 2.7 for 中已经存在Iterable)。
See this blog post "Strings" by Jesse, for 2.7 mkstring()examples:
有关2.7示例,请参阅Jesse 的这篇博客文章“字符串”mkstring():
/*
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
回答by Jasper
You could also combine Iterator.map()with mkString(), for example to create a query-string from a map[String,String]:
您还可以Iterator.map()与结合使用mkString(),例如从 a 创建查询字符串map[String,String]:
val queryString = updatedMap.map(pair => pair._1+"="+pair._2).mkString("?","&","")

