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

  • 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.
主站蜘蛛池模板: 平和县| 呼和浩特市| 沙田区| 墨玉县| 泊头市| 宣城市| 峨边| 宣武区| 犍为县| 上蔡县| 晴隆县| 清流县| 鄄城县| 淄博市| 湄潭县| 任丘市| 肃宁县| 高台县| 巴彦县| 淮北市| 特克斯县| 泸定县| 财经| 来宾市| 奇台县| 亳州市| 渝北区| 定日县| 当阳市| 石泉县| 治县。| 元朗区| 嘉义市| 青岛市| 资中县| 通辽市| 阿拉尔市| 五家渠市| 江北区| 开鲁县| 柳州市|