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. Use the multi.select() group to select items. To select items passing a given predicate, use multi.select().where(predicate):

List<Integer> list = multi
        .select().where(i -> i > 6)
        .collect().asList()
        .await().indefinitely();

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

The predicate passed to where is synchronous. The when method provides an asynchronous version:

List<Integer> list2 = multi
        .select().when(i -> Uni.createFrom().item(i > 6))
        .collect().asList()
        .await().indefinitely();

when accepts a function called for each item. Unlike where 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.