Scala SortedSet filter() 方法与示例
filter()方法用于选择 SortedSet 中满足指定谓词的所有元素。
Method Definition: def filter(p: (A) => Boolean): SortedSet[A]
Return Type: It returns a TreeSet containing all the elements of the SortedSet which satisfies the given predicate.
示例 #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()