Scala Language Collections Map and Filter Over A Collection


Example

Map

'Mapping' across a collection uses the map function to transform each element of that collection in a similar way. The general syntax is:

val someFunction: (A) => (B) = ???
collection.map(someFunction)

You can provide an anonymous function:

collection.map((x: T) => /*Do something with x*/)

Multiplying integer numbers by two

// Initialize 
val list = List(1,2,3)
// list: List[Int] = List(1, 2, 3)

// Apply map
list.map((item: Int) => item*2)
// res0: List[Int] = List(2, 4, 6)

// Or in a more concise way
list.map(_*2)
// res1: List[Int] = List(2, 4, 6)

Filter

filter is used when you want to exclude or 'filter out' certain elements of a collection. As with map, the general syntax takes a function, but that function must return a Boolean:

val someFunction: (a) => Boolean = ???
collection.filter(someFunction)

You can provide an anonymous function directly:

collection.filter((x: T) => /*Do something that returns a Boolean*/)

Checking pair numbers

val list = 1 to 10 toList
// list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

// Filter out all elements that aren't evenly divisible by 2
list.filter((item: Int) => item % 2==0)
// res0: List[Int] = List(2, 4, 6, 8, 10)

More Map and Filter examples

case class Person(firstName: String,
                  lastName: String,
                  title: String)

// Make a sequence of people
val people = Seq(
  Person("Millie", "Fletcher", "Mrs"),
  Person("Jim", "White", "Mr"),
  Person("Jenny", "Ball", "Miss") )


// Make labels using map
val labels = people.map( person =>
  s"${person.title}. ${person.lastName}"
)

// Filter the elements beginning with J
val beginningWithJ = people.filter(_.firstName.startsWith("J"))

// Extract first names and concatenate to a string
val firstNames = people.map(_.firstName).reduce( (a, b) => a + "," + b )