Scala Mutable SortedMap 迭代器方法与示例
迭代器方法用于给出一个迭代器。
Method Definition: def iterator: Iterator[(A, B)]
Return Type: It returns a non-empty iterator for non-empty SortedMap and returns an empty iterator for empty SortedMap.
示例 #1:
// Scala program of iterator
// method
import scala.collection.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3, "cs" -> 2)
// Applying iterator method
val result = m1.iterator
// Displays output
println(result)
}
}
输出:
non-empty iterator
示例 #2:
// Scala program of iterator
// method
import scala.collection.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedMap
val m1 = SortedMap( "cs" -> 2)
// Applying iterator method
val result = m1.iterator
// Displays output
println(result)
}
}
输出:
non-empty iterator