Scala SortedSet filter()方法及示例
filter() 方法是用来选择SortedSet中所有满足指定谓词的元素。
方法定义:def filter(p: (A) => Boolean):SortedSet[A]
返回类型。它返回一个TreeSet,其中包含满足给定谓词的所有SortedSet的元素。
例子 #1:
// Scala program of filter()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedSet
val s1 = SortedSet(5, 12, 3, 13)
// Applying filter method
val result = s1.filter(_ < 10)
// Displays output
println(result)
}
}
输出。
TreeSet(3, 5)
例子#2。
// Scala program of filter()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedSet
val s1 = SortedSet(5, 12, 3, 13)
// Applying filter method
val result = s1.filter(_ < 3)
// Displays output
println(result)
}
}
输出。
TreeSet()
极客教程