Scala Iterator filterNot()方法与实例
filterNot()方法属于AbstractIterator类的具体数值成员。它被定义在Iterator和IterableOnceOps类中。它选择所述Iterator中不满足给定谓词的所有元素。
方法定义:def filterNot(p: (A) => Boolean):Iterator[A]
返回类型:它返回一个新的Iterator,包含所述Iterator中不满足给定谓词p的所有元素。
例子 #1:
// Scala program of filterNot()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating an Iterator
val iter = Iterator(3, 9, 5, 1, 13)
// Applying filterNot method
val x = iter.filterNot(x => {x % 3 != 0})
// Applying next method
val result = x.next()
// Displays output
println(result)
}
}
输出。
3
例子#2。
// Scala program of filterNot()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating an Iterator
val iter = Iterator(2, 4, 5, 1, 13)
// Applying filterNot method
val x = iter.filterNot(x => {x % 2 == 0})
// Applying next method
val result = x.next()
// Displays output
println(result)
}
}
输出。
5
极客教程