Scala List filter() method with example
The filter() method is utilized to select all elements of the list which satisfies a stated predicate.
Method Definition: def filter(p: (A) => Boolean): List[A]
Return Type: It returns a new list consisting all the elements of the list which satisfies the given predicate.
Example #1:
object GfG
{
def main(args : Array[String])
{
val m 1 = List( 5 , 12 , 3 , 13 )
val result = m 1 .filter( _ < 10 )
println(result)
}
}
|
Example #2:
object GfG
{
def main(args : Array[String])
{
val m 1 = List( 5 , 12 , 3 , 13 )
val result = m 1 .filter( _ < 3 )
println(result)
}
}
|
Here, an empty list is returned as none of the elements satisfy the stated predicate.
Last Updated :
26 Jul, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...