官术网_书友最值得收藏!

  • 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.
主站蜘蛛池模板: 上高县| 辽阳市| 高清| 班戈县| 板桥市| 临夏县| 皋兰县| 永川市| 惠安县| 镇雄县| 筠连县| 孙吴县| 尉犁县| 绵阳市| 汝阳县| 临漳县| 平远县| 涞源县| 壤塘县| 宝山区| 富平县| 鱼台县| 航空| 政和县| 吉安市| 和平县| 达日县| 明光市| 曲麻莱县| 四平市| 南汇区| 绥棱县| 清河县| 淳安县| 盐源县| 介休市| 科技| 沅江市| 蓝田县| 平邑县| 涟源市|