Scala Set filter() method with example
The filter() method is utilized to select all elements of the set which satisfies a stated predicate.
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.
Example #1:
// Scala program of filter() // method // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating a set val s 1 = Set( 5 , 12 , 3 , 13 ) // Applying filter method val result = s 1 .filter( _ < 10 ) // Displays output println(result) } } |
Output:
Set(5, 3)
Example #2:
// Scala program of filter() // method // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating a set val s 1 = Set( 5 , 12 , 3 , 13 ) // Applying filter method val result = s 1 .filter( _ < 3 ) // Displays output println(result) } } |
Output:
Set()
Please Login to comment...