banner



How To Write Multiple Filter Conditions In Scala

Scala filter

Introduction to Scala filter

Scala filter is a method that is used to select the values in an elements or collection by filtering information technology with a certain condition. The Scala filter method takes up the status equally the parameter which is a Boolean value and returns the outcome after filtering over that condition. Whatever values that satisfies that status is given as the output result and the ane non 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 listing with the filter status containing the values that satisfies the status.

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 any is mentioned and filters the information then accordingly. It commencement checks the status for the Boolean value if it is truthful it filters the data based on that detail value and and then publishes the event. The filter office walks through the all elements in a collection, we just need to pass the logic or the algorithm by which we need to apply the filter method. We tin can laissez passer complex logics besides as methods besides inside an filter method. We can also use filter in the same code or logic as many times as we want to.

Example #i

Code:

scala> val a = List(3,4,5,half dozen,7,8)
a: List[Int] = List(3, 4, 5, 6, 7, 8)
Here we created an List with the values stored in a and practical the filter method over that, that gives upward all the values greater than 6.
scala> a.filter(10=>x>six)
res4: List[Int] = List(seven, 8)

Example #two

Code:

scala> val b = Listing("Arpit","Anand")
b: List[String] = List(Arpit, Anand)
scala> b.filter(10=>10.equalsIgnoreCase("Arpit"))
res11: Listing[Cord] = List(Arpit)
scala> b.filter(ten=>x.equalsIgnoreCase("Arp"))
res12: List[String] = List()

And then hither we saw if the particular status is non fulfilled so an empty listing value is returned.

Output:

Scala filter 1

Example #3

Using more more filter over the same collection.

Code:

scala> val a = List(three,4,5,half dozen,7,8)
a: List[Int] = List(three, iv, 5, 6, seven, 8)
scala> a.filter(x=>ten>vi).filter(ten=>x<8)
res14: List[Int] = List(vii)
scala> a.filter(x=>ten>6).filter(x=>ten>eight)
res17: List[Int] = List()

If any of the status fails it returns an nix or empty value.

Output:

Scala filter 2

Example #four

We can even pass the map function as well over the filtered values to go desired results.

Lawmaking:

scala> val a = List(three,4,5,6,7,8)
a: List[Int] = List(iii, 4, 5, 6, 7, 8)
scala> a.filter(x=>x>6).map(x=>x*2)
res22: Listing[Int] = List(fourteen, xvi)

This first filters out the information 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 part works in listing tin can be used over map function as well. We tin can filter values based on cardinal also every bit values over a map part.

Code:

scala> val c = Map("Arpit"->2,"Anand"->3)
c: scala.collection.immutable.Map[String,Int] = Map(Arpit -> ii, Anand -> iii)
A Map is created with two values in information technology.
scala> c.filter(x=>x._1 == "A")
res31: scala.drove.immutable.Map[String,Int] = Map()
We apply the filter status to check and if it satisfies it produces the result or else Null is returned.
scala> c.filter(10=>x._1 == "Arpit")
res32: scala.collection.immutable.Map[Cord,Int] = Map(Arpit -> two)
scala> c.filter(x=>10._1 == "Arpit" && ten._2 == 3)
res33: scala.collection.immutable.Map[Cord,Int] = Map()
scala> c.filter(x=>x._1 == "Arpit" && ten._2 == 2)
res34: scala.collection.immutable.Map[String,Int] = Map(Arpit -> two)

Output:

Scala filter 3

Then from these above examples we saw how we tin use the filter over the map Function. We can likewise use it with other collections in Scala.

Case #6

Some More Operations with Filter Function.

a. We can utilize the various methods with the filter function in it, it only works like it takes upward the filtered value and produces the consequence.

Using Nothing with Filter:

Code:

scala> val a = List(3,iv,v,half dozen,7,8)
a: List[Int] = List(iii, iv, five, 6, 7, 8)
scala> val b = List(6,vii,89)
b: Listing[Int] = List(six, 7, 89)
scala> a.filter(10=>x>6) null b
res36: Listing[(Int, Int)] = Listing((7,six), (eight,seven))
scala> a.filter(x=>x>4) zip b
res37: Listing[(Int, Int)] = List((v,half dozen), (half dozen,7), (seven,89))

b. Nosotros tin also check the length after applying the filter function and traverse the new drove with the values in it.

Code:

scala> a.filter(x=>10>vi).length
res38: Int = 2
scala> a.filter(x=>x>4).length
res39: Int = four
scala> a.filter(x=>x>4).foreach(println)
five
vi
7
8

Output:

traverse the new collection with the values in it

c. We can also discover the first occurrence of any condition inside the filter method with the help of find function with filter.

Code:

scala> val a = List(iii,4,5,half-dozen,vii,8)
a: List[Int] = List(3, 4, 5, 6, 7, 8)
scala> a.filter(10=>x>3).observe(x=>ten>4)
res49: Option[Int] = Some(5)
scala> a.filter(x=>x>iii).find(ten=>x>6)
res50: Option[Int] = Some(vii)
scala> a.filter(x=>x>three).find(ten=>x>=six)
res51: Option[Int] = Some(vi)

d. So merely every bit nosotros do functioning over list or map or any collection class in Scala we same tin 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 course in Scala.

Code:

scala> a.filter(x=>x>3).max
res52: Int = 8
scala> a.filter(10=>x>iii).min
res53: Int = four
scala> a.filter(x=>x>3).sum
res54: Int = 30

Output:

Methods like Max, Min , sum

And so from the above commodity we found how filter operation can be done over the drove course and diverse methods can be applied over that to achieve the result.

Conclusion

From the in a higher place article we saw how we tin use the Scala filter method and checked the syntax and return blazon for the same. Nosotros also saw with the help of examples how the filter operation is applied to a drove and the methods we can use over the filter operation. So past this nosotros can conclude that Scala filter method is an important method in Scala object oriented programming to filter out the information in a lawmaking efficiently.

Recommended Manufactures

This is a guide to Scala filter. Here we discuss the introduction to Scala filter along with the working and respective programming examples for better understanding. Y'all may likewise have a look at the following articles to learn more –

  1. Scala Queue
  2. Scala Trait
  3. Scala if else
  4. Scala Data Types

How To Write Multiple Filter Conditions In Scala,

Source: https://www.educba.com/scala-filter/

Posted by: smithloond1969.blogspot.com

0 Response to "How To Write Multiple Filter Conditions In Scala"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel