Scala Set toMap()方法及示例
toMap() 方法被用来返回一个由集合的所有元素组成的Map。
方法定义:def toMap[T, U]:Map[T, U]
返回类型。它返回一个由集合的所有元素组成的Map。
例子 #1:
// Scala program of toMap()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set((1, 2), (3, 4), (5, 6))
// Applying toMap method
val result = s1.toMap
// Display output
println(result)
}
}
输出。
Map(1 -> 2, 3 -> 4, 5 -> 6)
例子#2。
// Scala program of toMap()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set((12, 2), (13, 4), (15, 6))
// Applying toMap method
val result = s1.toMap
// Display output
println(result)
}
}
输出。
Map(12 -> 2, 13 -> 4, 15 -> 6)
极客教程