带有示例的 Scala 迭代器 toSet() 方法
toSet()方法属于抽象迭代器类的具体值成员。它在类IterableOnceOps中定义。
Method Definition: def toSet[B >: A]: immutable.Set[B]
Return Type: It returns a set from the stated collection.
示例 #1:
// Scala program of toSet()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a Iterator
val iter = Iterator(5, 6, 8, 9)
// Applying toSet method
val result = iter.toSet
// Displays output
println(result)
}
}
输出:
Set(5, 6, 8, 9)
示例 #2:
// Scala program of toSet()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating an empty Iterator
val iter = Iterator()
// Applying toSet method
val result = iter.toSet
// Displays output
println(result)
}
}
输出:
Set()