RxJava takeUntil with emmision of last item?

谁都会走 提交于 2019-12-22 04:06:12

问题


Is there a possibility to emit item that meets condition in takeUntil operator?


回答1:


Mmmm not sure if I understand your question. Something like this?

@Test
public void tesTakeUntil() {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    Observable.from(numbers)
              .takeUntil(number -> number > 3)
              .subscribe(System.out::println);

}

it will print

 1
 2
 3
 4

You can see more examples of Take here https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/filtering/ObservableTake.java




回答2:


final String stop = "c";
Observable.just("a", "b", "c", "d")
          .filter(item -> !item.equals(stop))
          .takeUntil(item -> item.equals(stop))
          .subscribe(System.out::println);

Output:

c



回答3:


With this solution, the predicate only has to be called once.

final String stop = "c";
Observable.just("a", "b", "c", "d")
          .takeUntil(item -> item.equals(stop))
          .lastElement()
          .subscribe(System.out::println);

Output:

c


来源:https://stackoverflow.com/questions/38183494/rxjava-takeuntil-with-emmision-of-last-item

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!