- 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.
推薦閱讀
- Microsoft Dynamics 365 Extensions Cookbook
- Animate CC二維動畫設計與制作(微課版)
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優化計算
- C++ 從入門到項目實踐(超值版)
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- Advanced Express Web Application Development
- Getting Started with Python
- Delphi開發典型模塊大全(修訂版)
- 程序員必會的40種算法
- Get Your Hands Dirty on Clean Architecture
- Visual FoxPro數據庫程序設計
- Azure for Architects
- Mobile Test Automation with Appium
- 軟件測試
- Python Django Web典型模塊開發實戰