带有开头、分隔符和结尾的 Scala SortedMap 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 SortedMap 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
import scala.collection.immutable.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3, "cs" -> 3)
// Applying addString method
val result = m1.addString(new StringBuilder(), ">>>", "|", "--")
// Displays output
println(result)
}
}
输出:
>>>cs -> 3|for -> 3|geeks -> 5--
示例 #2:
// Scala program of addString() method
// with a start, a separator and an
// end
import scala.collection.immutable.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3, "for" -> 3)
// Applying addString method
val result = m1.addString(new StringBuilder(), ">>>", "|", "--")
// Displays output
println(result)
}
}
输出:
>>>for -> 3|geeks -> 5--
因此,这里删除了相同的键。