带有示例的 Scala SortedMap addString() 方法
addString()方法用于将 SortedMap 的元素添加到 String Builder。
Method Definition: def addString(b: StringBuilder): StringBuilder
Return Type: It returns the elements in the String Builder.
示例 #1:
// Scala program of addString()
// method
import scala.collection.immutable.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3, "cs" -> 2)
// Applying addString method
val result = m1.addString(new StringBuilder())
// Displays output
println(result)
}
}
输出:
cs -> 2for -> 3geeks -> 5
示例 #2:
// Scala program of addString()
// method
import scala.collection.immutable.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3, "cs" -> 2, "geeks" -> 5)
// Applying addString method
val result = m1.addString(new StringBuilder())
// Displays output
println(result)
}
}
输出:
cs -> 2for -> 3geeks -> 5
因此,我们可以在这里看到相同的密钥只被使用一次。