Scala List filterNot() operation with examples

 173 total views,  1 views today

Welcome to Scala series. In this tutorial, you will learn about the Scala List filterNot() operation with examples.

Scala filterNot() operation is a useful operation of the Scala List or any collection which allows us to filter elements from the list or collection which is not satisfying given predicate function. When you call this filterNot() operation on a Scala list, it returns a new collection by filtering out the elements which do not satisfies the given predicate or boolean function.

filterNot() Syntax

def filterNot(p: (A) => Boolean): List[A]

Selects all elements of this list which do not satisfy a predicate.

Scala List filterNot() examples

In this filterNot() example, We have created a list of strings which is of different sizes. We are applying filterNot() operation on the list based on a given condition. i.e., filter elements that don’t have a length of 2.The result list will have all elements which are not of length 2. It only selects the elements that are not satisfying given predicate.

filterNot() operation can be applied to any collection in Scala.

object FilterNotTest {

  def main(args: Array[String]): Unit = {
    //create a List
    var list: List[String] = List("aa", "bbb", "cc", "ddd")

    //Filter elements which doesn't have size 2.
    list.filterNot(ele => ele.length() == 2).foreach(println _)
  }
}

Output

bbb
ddd

Let us see another example based on Even or odd numbers in the collection.

In this filterNot() example, we have created a list of integers.filterNot() operation is applied on the list to filter out only odd  numbers. The predicate operation should not satisfy. The result collection will have elements that are odd numbers.

object FilterNotTest {

  def main(args: Array[String]): Unit = {
    //create a List
    var list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

    //Filter elements which are odd
    list.filterNot(ele => ele % 2 == 0).foreach(println _)
  }
}

Output

1
3
5
7
9
11

FilterNot() even number example

object FilterNotTest {

  def main(args: Array[String]): Unit = {
    //create a List
    var list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

    //Filter elements which are even
    list.filterNot(ele => ele % 2 != 0).foreach(println _)
  }
}

Output

2
4
6
8
10
12

ReferencesScala filterNot() operation

I hope you like this tutorial and you were able to understand Scala list filterNot() operation with examples.

Thanks for reading and please give us a thumb up and comment below.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.