Filter

filter selects all the elements that satisfy a specific predicate. It can be defined formally as follows:

def filter(p: (A) ⇒ Boolean): Traversable[A]  

Let's see an example as follows:

scala> //Given a list of tuples (cities, Populations)
scala> // Get all cities that has population more than 5 million
scala> List(("Dublin", 2), ("NY", 8), ("London", 8)) filter (x =>x._2 >= 5)
res3: List[(String, Int)] = List((NY,8), (London,8))

A map is used to build a new collection or set of elements by traversing a function to all elements of the collection. In the next subsection, we will see an example of using Map.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.18.109.154