I have to convert Map to string with given 2 delimiters and I wanted to use my own delimiter
I have done with the below code
Map("ss"-> "yy", "aa"-> "bb").map(data => s"${data._1}:${data._2}").mkString("|")
The out out is ss:yy|aa:bb
I'm looking for the better way.
I believe that mkString
is the right way of concatenating strings with delimiters. You can apply it to the tuples as well for uniformity, using productIterator
:
Map("ss"-> "yy", "aa"-> "bb")
.map(_.productIterator.mkString(":"))
.mkString("|")
Note, however, that productIterator
loses type information. In the case of strings that won't cause much harm, but can matter in other situations.
Map("ss" -> "yy", "aa" -> "bb").map{case (k, v) => k + ":" + v}.mkString("|")
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