Scala Map toSet()方法及示例
toSet()方法被用来显示Scala Map中的一个集合。
方法定义: def toSet:Set[A]
返回类型。它从所述地图中返回一个集合。
例子 #1:
// Scala program of toSet()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a map
val m1 = Map(3 -> "geeks", 4 -> "for", 4 -> "for")
// Applying toSet method
val result = m1.toSet
// Displays output
println(result)
}
}
输出。
Set((3, geeks), (4, for))
例子#2。
// Scala program of toSet()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a map
val m1 = Map(3 -> "geeks", 4 -> "for", 2 -> "cs")
// Applying toSet method
val result = m1.toSet
// Displays output
println(result)
}
}
输出。
Set((3, geeks), (4, for), (2, cs))
极客教程