blockingGet()

Maybe and Single do not have blockingFirst() since there can only be one element at most. Logically, for a Single and Maybe, it is not exactly the first element, but rather the only element, so the equivalent operator is blockingGet().

Here, we assert that all items of length four include only Beta and Zeta, and we collect them with toList(), which yields a Single<List<String>>. We can use blockingGet() to wait for this list and assert that it is equal to our desired result:

 import io.reactivex.Observable;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertTrue;

public class RxTest {

@Test
public void testSingle() {
Observable<String> source =
Observable.just("Alpha", "Beta", "Gamma", "Delta", "Zeta");

List<String> allWithLengthFour = source.filter(s -> s.length() == 4)
.toList()
.blockingGet();

assertTrue(allWithLengthFour.equals(Arrays.asList("Beta","Zeta")));
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.10.2