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