Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava takeUntil with emmision of last item?

Tags:

java

rx-java

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

like image 568
pixel Avatar asked Jul 04 '16 11:07

pixel


3 Answers

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

like image 131
paul Avatar answered Sep 27 '22 17:09

paul


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
like image 35
veyndan Avatar answered Sep 27 '22 18:09

veyndan


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
like image 33
yurgis Avatar answered Sep 27 '22 16:09

yurgis