Scala Set forall()方法及示例

Scala Set forall()方法及示例

forall() 方法用来检查给定的谓词是否满足集合的所有元素。

方法定义: def forall(p: (A) => Boolean):Boolean

返回类型。如果所述谓词对集合中的所有元素都成立,则返回真,否则返回假。

例子 #1:

// Scala program of forall() 
// method 
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a set 
        val s1 = Set(3, 6, 12, 9, 21) 
          
        // Applying forall method 
        val result = s1.forall(y => {y % 3 == 0}) 
          
        // Displays output 
        println(result) 
      
    } 
} 

输出。

true

例子#2。

// Scala program of forall() 
// method 
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a set 
        val s1 = Set(3, 7, 12, 9, 21) 
          
        // Applying forall method 
        val result = s1.forall(y => {y % 3 == 0}) 
          
        // Displays output 
        println(result) 
      
    } 
} 

输出。

false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程