rxjava - how to take alternating emissions from two observables











up vote
0
down vote

favorite












I have two observables and i would like to take from each one until there is nothing left from both. is there any operator to do this ? let me show you an example of what i have tried and what i want to accomplish:



  Observable<String> observable = Observable.just("hello","are", "doing");
Observable<String> observable2 = Observable.just("how","you","today");


i would like to "reduce" this such that the final emissions would be"
Hello how are you doing today



here i have tried using scan but it works more like accumulation and i get the following results:



Observable.merge(observable, observable2).reduce(new BiFunction<String, String, String>() {
@Override
public String apply(String wordAccum1, String word2) {
return wordAccum1 + " " + word2;
}

}).subscribe(new Consumer<String>() {
@Override
public void accept(String sentence) throws Exception {
Log.v("consumerResult",sentence+"");
}
});


log output shows:
consumerResult: hello are doing how you today



how can i take from each one consecutively ?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have two observables and i would like to take from each one until there is nothing left from both. is there any operator to do this ? let me show you an example of what i have tried and what i want to accomplish:



      Observable<String> observable = Observable.just("hello","are", "doing");
    Observable<String> observable2 = Observable.just("how","you","today");


    i would like to "reduce" this such that the final emissions would be"
    Hello how are you doing today



    here i have tried using scan but it works more like accumulation and i get the following results:



    Observable.merge(observable, observable2).reduce(new BiFunction<String, String, String>() {
    @Override
    public String apply(String wordAccum1, String word2) {
    return wordAccum1 + " " + word2;
    }

    }).subscribe(new Consumer<String>() {
    @Override
    public void accept(String sentence) throws Exception {
    Log.v("consumerResult",sentence+"");
    }
    });


    log output shows:
    consumerResult: hello are doing how you today



    how can i take from each one consecutively ?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have two observables and i would like to take from each one until there is nothing left from both. is there any operator to do this ? let me show you an example of what i have tried and what i want to accomplish:



        Observable<String> observable = Observable.just("hello","are", "doing");
      Observable<String> observable2 = Observable.just("how","you","today");


      i would like to "reduce" this such that the final emissions would be"
      Hello how are you doing today



      here i have tried using scan but it works more like accumulation and i get the following results:



      Observable.merge(observable, observable2).reduce(new BiFunction<String, String, String>() {
      @Override
      public String apply(String wordAccum1, String word2) {
      return wordAccum1 + " " + word2;
      }

      }).subscribe(new Consumer<String>() {
      @Override
      public void accept(String sentence) throws Exception {
      Log.v("consumerResult",sentence+"");
      }
      });


      log output shows:
      consumerResult: hello are doing how you today



      how can i take from each one consecutively ?










      share|improve this question















      I have two observables and i would like to take from each one until there is nothing left from both. is there any operator to do this ? let me show you an example of what i have tried and what i want to accomplish:



        Observable<String> observable = Observable.just("hello","are", "doing");
      Observable<String> observable2 = Observable.just("how","you","today");


      i would like to "reduce" this such that the final emissions would be"
      Hello how are you doing today



      here i have tried using scan but it works more like accumulation and i get the following results:



      Observable.merge(observable, observable2).reduce(new BiFunction<String, String, String>() {
      @Override
      public String apply(String wordAccum1, String word2) {
      return wordAccum1 + " " + word2;
      }

      }).subscribe(new Consumer<String>() {
      @Override
      public void accept(String sentence) throws Exception {
      Log.v("consumerResult",sentence+"");
      }
      });


      log output shows:
      consumerResult: hello are doing how you today



      how can i take from each one consecutively ?







      rx-java2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 15:30

























      asked Nov 19 at 12:20









      j2emanue

      20.8k22135244




      20.8k22135244
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          Try this



              Observable<String> observable = Observable.just("hello", "are", "doing");
          Observable<String> observable2 = Observable.just("how", "you", "today");


          observable.zipWith(observable2, new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+" "+s2;
          }
          }).reduce(new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+ " " + s2;
          }
          }).subscribe(new Consumer<String>() {
          @Override
          public void accept(String s) throws Exception {
          System.out.println(s); // this will emit with "hello how are you doing today"
          }
          });


          If you are using Java 8, you can reduce the same code using lambda, as follows



           observable.zipWith(observable2, (s, s2) -> s + " " + s2)
          .reduce((s, s2) -> s +" "+ s2).subscribe(System.out::println);





          share|improve this answer



















          • 1




            The key is zipWith. You don't need scan+lastElement, reduce suffices.
            – akarnokd
            Nov 19 at 14:10










          • @akarnokd updated the answer with reduce
            – Sarath Kn
            Nov 19 at 14:15











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374508%2frxjava-how-to-take-alternating-emissions-from-two-observables%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          Try this



              Observable<String> observable = Observable.just("hello", "are", "doing");
          Observable<String> observable2 = Observable.just("how", "you", "today");


          observable.zipWith(observable2, new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+" "+s2;
          }
          }).reduce(new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+ " " + s2;
          }
          }).subscribe(new Consumer<String>() {
          @Override
          public void accept(String s) throws Exception {
          System.out.println(s); // this will emit with "hello how are you doing today"
          }
          });


          If you are using Java 8, you can reduce the same code using lambda, as follows



           observable.zipWith(observable2, (s, s2) -> s + " " + s2)
          .reduce((s, s2) -> s +" "+ s2).subscribe(System.out::println);





          share|improve this answer



















          • 1




            The key is zipWith. You don't need scan+lastElement, reduce suffices.
            – akarnokd
            Nov 19 at 14:10










          • @akarnokd updated the answer with reduce
            – Sarath Kn
            Nov 19 at 14:15















          up vote
          5
          down vote



          accepted










          Try this



              Observable<String> observable = Observable.just("hello", "are", "doing");
          Observable<String> observable2 = Observable.just("how", "you", "today");


          observable.zipWith(observable2, new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+" "+s2;
          }
          }).reduce(new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+ " " + s2;
          }
          }).subscribe(new Consumer<String>() {
          @Override
          public void accept(String s) throws Exception {
          System.out.println(s); // this will emit with "hello how are you doing today"
          }
          });


          If you are using Java 8, you can reduce the same code using lambda, as follows



           observable.zipWith(observable2, (s, s2) -> s + " " + s2)
          .reduce((s, s2) -> s +" "+ s2).subscribe(System.out::println);





          share|improve this answer



















          • 1




            The key is zipWith. You don't need scan+lastElement, reduce suffices.
            – akarnokd
            Nov 19 at 14:10










          • @akarnokd updated the answer with reduce
            – Sarath Kn
            Nov 19 at 14:15













          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          Try this



              Observable<String> observable = Observable.just("hello", "are", "doing");
          Observable<String> observable2 = Observable.just("how", "you", "today");


          observable.zipWith(observable2, new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+" "+s2;
          }
          }).reduce(new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+ " " + s2;
          }
          }).subscribe(new Consumer<String>() {
          @Override
          public void accept(String s) throws Exception {
          System.out.println(s); // this will emit with "hello how are you doing today"
          }
          });


          If you are using Java 8, you can reduce the same code using lambda, as follows



           observable.zipWith(observable2, (s, s2) -> s + " " + s2)
          .reduce((s, s2) -> s +" "+ s2).subscribe(System.out::println);





          share|improve this answer














          Try this



              Observable<String> observable = Observable.just("hello", "are", "doing");
          Observable<String> observable2 = Observable.just("how", "you", "today");


          observable.zipWith(observable2, new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+" "+s2;
          }
          }).reduce(new BiFunction<String, String, String>() {
          @Override
          public String apply(String s, String s2) throws Exception {
          return s+ " " + s2;
          }
          }).subscribe(new Consumer<String>() {
          @Override
          public void accept(String s) throws Exception {
          System.out.println(s); // this will emit with "hello how are you doing today"
          }
          });


          If you are using Java 8, you can reduce the same code using lambda, as follows



           observable.zipWith(observable2, (s, s2) -> s + " " + s2)
          .reduce((s, s2) -> s +" "+ s2).subscribe(System.out::println);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 14:20

























          answered Nov 19 at 14:09









          Sarath Kn

          1,500918




          1,500918








          • 1




            The key is zipWith. You don't need scan+lastElement, reduce suffices.
            – akarnokd
            Nov 19 at 14:10










          • @akarnokd updated the answer with reduce
            – Sarath Kn
            Nov 19 at 14:15














          • 1




            The key is zipWith. You don't need scan+lastElement, reduce suffices.
            – akarnokd
            Nov 19 at 14:10










          • @akarnokd updated the answer with reduce
            – Sarath Kn
            Nov 19 at 14:15








          1




          1




          The key is zipWith. You don't need scan+lastElement, reduce suffices.
          – akarnokd
          Nov 19 at 14:10




          The key is zipWith. You don't need scan+lastElement, reduce suffices.
          – akarnokd
          Nov 19 at 14:10












          @akarnokd updated the answer with reduce
          – Sarath Kn
          Nov 19 at 14:15




          @akarnokd updated the answer with reduce
          – Sarath Kn
          Nov 19 at 14:15


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374508%2frxjava-how-to-take-alternating-emissions-from-two-observables%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python