filterNot

Filter the incoming elements using a predicate.

Simple operators

Signature

Source.filterNotSource.filterNot Flow.filterNotFlow.filterNot

Description

Filter the incoming elements using a predicate. If the predicate returns false the element is passed downstream, if it returns true the element is discarded.

See also filter.

Example

For example, given a Source of words we can omit the shorter words with the filterNot operator:

Scala
sourceval words: Source[String, NotUsed] =
  Source(
    ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt " +
    "ut labore et dolore magna aliqua").split(" ").toList)

val longWords: Source[String, NotUsed] = words.filterNot(_.length <= 6)

longWords.runForeach(println)
// consectetur
// adipiscing
// eiusmod
// incididunt
Java
sourceSource<String, NotUsed> words =
    Source.from(
        Arrays.asList(
            ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt "
                    + "ut labore et dolore magna aliqua")
                .split(" ")));

Source<String, NotUsed> longWords = words.filterNot(w -> w.length() <= 6);

longWords.runForeach(System.out::println, system);
// consectetur
// adipiscing
// eiusmod
// incididunt

Reactive Streams semantics

emits when the given predicate returns false for the element

backpressures when the given predicate returns false for the element and downstream backpressures

completes when upstream completes

API docs

Flow.filterNotFlow.filterNot