Scala Map toSet()方法的示例
toSet()方法被用来展示Scala中映射的集合。
方法定义: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))
阅读更多:Scala 教程
极客教程