Scala Set toMap() 方法与示例
toMap()方法用于返回由集合的所有元素组成的映射。
Method Definition: def toMap[T, U]: Map[T, U]
Return Type: It returns a map consisting of all the elements of the set.
示例 #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)