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.
What do you guys think about it? What is your coding snippet for printing a Map except println?
Scala Map get() method with exampleThe get() method is utilized to give the value associated with the keys of the map. The values are returned here as an Option i.e, either in form of Some or None. Return Type: It returns the keys corresponding to the values given in the method as argument.
Scala Map empty() method with exampleThe empty() method is utilized to make the map empty. Return Type: It returns an empty map.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With