Scala Set filter() 方法与示例
filter()方法用于选择集合中满足指定谓词的所有元素。
Method Definition: def filter(p: (A) => Boolean): Set[A]
Return Type: It returns a set containing all the elements of the set which satisfies the given predicate.
示例 #1:
// Scala program of filter()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(5, 12, 3, 13)
// Applying filter method
val result = s1.filter(_ < 10)
// Displays output
println(result)
}
}
输出:
Set(5, 3)
示例 #2:
// Scala program of filter()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(5, 12, 3, 13)
// Applying filter method
val result = s1.filter(_ < 3)
// Displays output
println(result)
}
}
输出:
Set()