- Learning RxJava
- Thomas Nield
- 139字
- 2021-07-02 22:22:56
filter()
The filter() operator accepts Predicate<T> for a given Observable<T>. This means that you provide it a lambda that qualifies each emission by mapping it to a Boolean value, and emissions with false will not go forward.
For instance, you can use filter() to only allow string emissions that are not five characters in length:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
.filter(s -> s.length() != 5)
subscribe(s -> System.out.println("RECEIVED: " + s));
}
}
The output of the preceding code snippet is as follows:
RECEIVED: Beta
RECEIVED: Epsilon
The filter() function is probably the most commonly used operator to suppress emissions.
Note that if all emissions fail to meet your criteria, the returned Observable will be empty, with no emissions occurring before onComplete() is called.
推薦閱讀
- 密碼學(xué)原理與Java實(shí)現(xiàn)
- 征服RIA
- 用Flutter極速構(gòu)建原生應(yīng)用
- Hands-On RESTful Web Services with Go
- PhoneGap Mobile Application Development Cookbook
- 自然語言處理Python進(jìn)階
- 區(qū)塊鏈底層設(shè)計Java實(shí)戰(zhàn)
- 編程菜鳥學(xué)Python數(shù)據(jù)分析
- C和C++游戲趣味編程
- Unity 3D腳本編程:使用C#語言開發(fā)跨平臺游戲
- 深入分析GCC
- Docker:容器與容器云(第2版)
- Android嵌入式系統(tǒng)程序開發(fā)(基于Cortex-A8)
- 軟件設(shè)計模式(Java版)
- 讓Python遇上Office:從編程入門到自動化辦公實(shí)踐