Scala Tutorial - Learn How To Use Filter And FilterNot Functions

By Nadim Bahadoor | Last updated: March 16, 2018 at 14:26 pm

Overview

In this tutorial, we will learn how to use the filter and filterNot functions on collection data structures in Scala. The filter and filterNot functions are applicable to both Scala's Mutable and Immutable collection data structures.

 

The filter method takes a predicate function as its parameter and uses it to select all the elements in the collection which matches the predicate. It will return a new collection with elements that matched the predicate.

 

As per the Scala documentation, the definition of the filter method is as follows:


def filter(p: (A) ⇒ Boolean): Repr

 

The filterNot method is similar to the filter method except that it will create a new collection with elements that do not match the predicate function.

 

As per the Scala documentation, the definition of the filterNot method is as follows:

def filterNot(p: (A) ⇒ Boolean): Repr

 

The filter and filterNot method is a member of the TraversableLike trait.


Steps

1. How to initialize a Sequence of donuts

The code below shows how to initialize a Sequence of Donut elements of type String.


println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut", "Vanilla Donut")
println(s"Elements of donuts = $donuts")

 

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut, Vanilla Donut)

 

2. How to keep only Plain and Glazed Donuts using the filter method

The code below shows how to use the filter method to find Plain and Glazed donut elements in the Sequence and return a new collection which contains only these donut elements.


println("\nStep 2: How to keep only Plain and Glazed Donuts using the filter method")
val sequenceWithPlainAndGlazedDonut = donuts.filter { donutName =>
  donutName.contains("Plain") || donutName.contains("Glazed")
}
println(s"Sequence with Plain and Glazed donuts only = $sequenceWithPlainAndGlazedDonut")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to keep only Plain and Glazed Donuts using the filter method
Sequence with Plain and Glazed donuts only = List(Plain Donut, Glazed Donut)




 

3. How to filter out element Vanilla Donut using the filterNot function

The code below shows how to use filterNot to filter out Vanilla Donut element from the donut sequence and return a new donut sequence which does not contain element Vanilla Donut.



println("\nStep 3: How to filter out element Vanilla Donut using the filterNot function")
val sequenceWithoutVanillaDonut = donuts.filterNot(donutName => donutName == "Vanilla Donut" )
println(s"Sequence without vanilla donut = $sequenceWithoutVanillaDonut")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to filter out element Vanilla Donut using the filterNot function
Sequence without vanilla donut = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

This concludes our tutorial on Learn How To Use Filter And FilterNot Functions and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to initialize a Sequence of donuts
  • How to keep only Plain and Glazed Donuts using the filter method
  • How to filter out element Vanilla Donut using the filterNot function

Tip

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use find function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: