Scala ListSet filter() 方法与示例
在 Scala ListSet 中,filter() 方法用于选择 listSet 中满足指定谓词的所有元素。
Method Definition: def filter(p: (A) => Boolean): ListSet[A]
Return Type: It returns a new listset consisting all the elements of the listset which satisfies the given predicate.
示例 1:
// Scala program of filter()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating litset
val m1 = ListSet(1, 2, 3, 4, 5, 7)
// Applying filter method
val result = m1.filter(_>2)
// Displays output
println(result)
}
}
输出:
ListSet(3, 4, 5, 7)
示例 2:
// Scala program of filter()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating litset
val m1 = ListSet(1, 2, 3, 4, 5, 7)
// Applying filter method
val result = m1.filter(_<1)
// Displays output
println(result)
}
}
输出:
ListSet()