RxJava takeUntil with emmision of last item?

2020-08-26 03:20发布

问题:

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:

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


回答3:

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


标签: java rx-java