Filtering items from multi

Learn how to filter and select items from a multi.

When observing a Multi, you may want to not forward all the received items to your downstream. To filter or select items, you can use multi.transform().byFilteringItemsWith(predicate):

List<Integer> list = multi
        .transform().byFilteringItemsWith(i -> i > 6)
        .collectItems().asList()
        .await().indefinitely();

byFilteringItemsWith accepts a predicate called for each item. If the predicate returns true, the item propagated downstream. Otherwise, it drops the item.

The predicated passed to byFilteringItemsWith is synchronous. The byTestingItemsWith method provides an asynchronous version:

List<Integer> list2 = multi
        .transform().byTestingItemsWith(i -> Uni.createFrom().item(i > 6))
        .collectItems().asList()
        .await().indefinitely();

byFilteringItemsWith accepts a function called for each item. Unlike byFilteringItemsWith where the predicate returns a boolean synchronously, the function returns a Uni<Boolean>. It forwards the item downstream if the uni produced by the function emits true. Otherwise, it drops the item.