带有分隔符的 Scala Map mkString() 方法和示例
此方法与mkString()方法相同,但此处还添加了一个分隔符。
Method Definition: def mkString(sep: String): String
Return Type: It returns the elements of the map as a string along with the separator between them.
示例 #1:
// Scala program of mkString()
// method with a separator
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating map
val m1 = Map("geeks" -> 5, "for" -> 3, "cs" -> 2)
// Applying mkString method
val result = m1.mkString("/")
// Displays output
println(result)
}
}
输出:
geeks -> 5/for -> 3/cs -> 2
示例 #2:
// Scala program of mkString()
// method with a separator
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating map
val m1 = Map("geeks" -> 5, "for" -> 3, "geeks" -> 5)
// Applying mkString method
val result = m1.mkString("/")
// Displays output
println(result)
}
}
输出:
geeks -> 5/for -> 3