📜  Scala Iterator exists() 方法与示例

📅  最后修改于: 2022-05-13 01:54:31.408000             🧑  作者: Mango

Scala Iterator exists() 方法与示例

exists()方法属于Abstract Iterator类的具体值成员。它在类IterableOnceOps中定义。它测试使用的谓词是否适用于所述集合的至少一个元素。它不会因无限大小的集合而终止。

示例 #1:

// Scala program of exists()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an Iterator 
        val iter = Iterator(7, 3, 4, 11, 13)
          
        // Applying exists method
        val result = iter.exists(x => {x % 2 == 0})
          
        // Displays output
        println(result)
      
    }
}
输出:
true

示例 #2:

// Scala program of exists()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an Iterator 
        val iter = Iterator(7, 3, 5, 11, 13)
          
        // Applying exists method
        val result = iter.exists(x => {x % 2 == 0})
          
        // Displays output
        println(result)
          
    }
}
输出:
false