Scala Tutorial - Learn How To Use Filter And FilterNot Functions
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)
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
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.