Updated April 11, 2023
Introduction to Scala filter
Scala filter is a method that is used to select the values in an elements or collection by filtering it with a certain condition. The Scala filter method takes up the condition as the parameter which is a Boolean value and returns the result after filtering over that condition. Whatever values that satisfies that condition is given as the output result and the one not is eradicated from the list. Scala filter method filters the data on a Boolean condition and produces the result.
Syntax:
def filter(p: (A) => Boolean): List[A]
It returns a new list with the filter condition containing the values that satisfies the condition.
def filter(p: ((A, B))=> Boolean): Map[A, B]
It returns a new map with the filter condition containing the values that satisfies the condition.
Working of Scala filter with Examples
Scala filter checks for the condition whatever is mentioned and filters the data then accordingly. It first checks the condition for the Boolean value if it is true it filters the data based on that particular value and then publishes the result. The filter function walks through the all elements in a collection, we just need to pass the logic or the algorithm by which we need to use the filter method. We can pass complex logics as well as methods also inside an filter method. We can also use filter in the same code or logic as many times as we want to.
Example #1
Code:
scala> val a = List(3,4,5,6,7,8)
a: List[Int] = List(3, 4, 5, 6, 7, 8)
Here we created an List with the values stored in a and applied the filter method over that, that gives up all the values greater than 6.
scala> a.filter(x=>x>6)
res4: List[Int] = List(7, 8)
Example #2
Code:
scala> val b = List("Arpit","Anand")
b: List[String] = List(Arpit, Anand)
scala> b.filter(x=>x.equalsIgnoreCase("Arpit"))
res11: List[String] = List(Arpit)
scala> b.filter(x=>x.equalsIgnoreCase("Arp"))
res12: List[String] = List()
So here we saw if the particular condition is not fulfilled then an empty list value is returned.
Output:
Example #3
Using more than more filter over the same collection.
Code:
scala> val a = List(3,4,5,6,7,8)
a: List[Int] = List(3, 4, 5, 6, 7, 8)
scala> a.filter(x=>x>6).filter(x=>x<8)
res14: List[Int] = List(7)
scala> a.filter(x=>x>6).filter(x=>x>8)
res17: List[Int] = List()
If any of the condition fails it returns an null or empty value.
Output:
Example #4
We can even pass the map function also over the filtered values to get desired results.
Code:
scala> val a = List(3,4,5,6,7,8)
a: List[Int] = List(3, 4, 5, 6, 7, 8)
scala> a.filter(x=>x>6).map(x=>x*2)
res22: List[Int] = List(14, 16)
This first filters out the data and then a map operation is performed with the filtered data.
Example #5
Using Filter Operation with Map Function.
The way what we checked how filter function works in list can be used over map function also. We can filter values based on key as well as values over a map function.
Code:
scala> val c = Map("Arpit"->2,"Anand"->3)
c: scala.collection.immutable.Map[String,Int] = Map(Arpit -> 2, Anand -> 3)
A Map is created with two values in it.
scala> c.filter(x=>x._1 == "A")
res31: scala.collection.immutable.Map[String,Int] = Map()
We use the filter condition to check and if it satisfies it produces the result or else Null is returned.
scala> c.filter(x=>x._1 == "Arpit")
res32: scala.collection.immutable.Map[String,Int] = Map(Arpit -> 2)
scala> c.filter(x=>x._1 == "Arpit" && x._2 == 3)
res33: scala.collection.immutable.Map[String,Int] = Map()
scala> c.filter(x=>x._1 == "Arpit" && x._2 == 2)
res34: scala.collection.immutable.Map[String,Int] = Map(Arpit -> 2)
Output:
So from these above examples we saw how we can use the filter over the map Function. We can also use it with other collections in Scala.
Example #6
Some More Operations with Filter Function.
a. We can use the various methods with the filter function in it, it just works like it takes up the filtered value and produces the result.
Using Zip with Filter:
Code:
scala> val a = List(3,4,5,6,7,8)
a: List[Int] = List(3, 4, 5, 6, 7, 8)
scala> val b = List(6,7,89)
b: List[Int] = List(6, 7, 89)
scala> a.filter(x=>x>6) zip b
res36: List[(Int, Int)] = List((7,6), (8,7))
scala> a.filter(x=>x>4) zip b
res37: List[(Int, Int)] = List((5,6), (6,7), (7,89))
b. We can also check the length after applying the filter function and traverse the new collection with the values in it.
Code:
scala> a.filter(x=>x>6).length
res38: Int = 2
scala> a.filter(x=>x>4).length
res39: Int = 4
scala> a.filter(x=>x>4).foreach(println)
5
6
7
8
Output:
c. We can also find the first occurrence of any condition inside the filter method with the help of find function with filter.
Code:
scala> val a = List(3,4,5,6,7,8)
a: List[Int] = List(3, 4, 5, 6, 7, 8)
scala> a.filter(x=>x>3).find(x=>x>4)
res49: Option[Int] = Some(5)
scala> a.filter(x=>x>3).find(x=>x>6)
res50: Option[Int] = Some(7)
scala> a.filter(x=>x>3).find(x=>x>=6)
res51: Option[Int] = Some(6)
d. So just as we do operation over list or map or any collection class in Scala we same can do by filtering the value thereafter using the .filter method in Scala.
Methods like Max, Min, Sum, etc can be used in the same way we use it with the other collection class in Scala.
Code:
scala> a.filter(x=>x>3).max
res52: Int = 8
scala> a.filter(x=>x>3).min
res53: Int = 4
scala> a.filter(x=>x>3).sum
res54: Int = 30
Output:
So from the above article we found how filter operation can be done over the collection class and various methods can be applied over that to achieve the result.
Conclusion
From the above article we saw how we can use the Scala filter method and checked the syntax and return type for the same. We also saw with the help of examples how the filter operation is applied to a collection and the methods we can use over the filter operation. So by this we can conclude that Scala filter method is an important method in Scala object oriented programming to filter out the data in a code efficiently.
Recommended Articles
We hope that this EDUCBA information on “Scala filter” was beneficial to you. You can view EDUCBA’s recommended articles for more information.