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

  • 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.
主站蜘蛛池模板: 诸暨市| 磴口县| 云梦县| 枣强县| 金华市| 阳泉市| 淅川县| 封开县| 雅安市| 千阳县| 五家渠市| 乐东| 龙胜| 元朗区| 舒兰市| 鞍山市| 蒙阴县| 郎溪县| 颍上县| 嘉兴市| 乌鲁木齐县| 于都县| 丰台区| 监利县| 普兰店市| 新龙县| 当阳市| 平原县| 五寨县| 神池县| 明星| 华蓥市| 南靖县| 林西县| 洪湖市| 石景山区| 新邵县| 洛宁县| 台江县| 白朗县| 宣化县|