带有开头、分隔符和结尾的 Scala Map addString() 方法示例
此方法与addString()方法相同,但这里还包括一个开始、一个分隔符和一个结束。
Method Definition: def addString(sb: mutable.StringBuilder, start: String, sep: String, end: String): mutable.StringBuilder
Where, sep is the separator stated.
Return Type: It returns the elements of the map in the String Builder and a start, a separator and an end is also included here.
示例 #1:
// Scala program of addString() method
// with a start, a separator and an
// end
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating map
val m1 = Map("geeks" -> 5, "for" -> 3, "cs" -> 3)
// Applying addString method
val result = m1.addString(new StringBuilder(), ">>>", "|", "--")
// Displays output
println(result)
}
}
输出:
>>>geeks -> 5|for -> 3|cs -> 3--
示例 #2:
// Scala program of addString() method
// with a start, a separator and an
// end
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating map
val m1 = Map("geeks" -> 5, "for" -> 3, "for" -> 3)
// Applying addString method
val result = m1.addString(new StringBuilder(), ">>>", "|", "--")
// Displays output
println(result)
}
}
输出:
>>>geeks -> 5|for -> 3--
因此,这里删除了相同的键。