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 ?
rx-java2
add a comment |
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 ?
rx-java2
add a comment |
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 ?
rx-java2
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
rx-java2
edited Nov 19 at 15:30
asked Nov 19 at 12:20
j2emanue
20.8k22135244
20.8k22135244
add a comment |
add a comment |
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);
1
The key iszipWith
. You don't needscan
+lastElement
,reduce
suffices.
– akarnokd
Nov 19 at 14:10
@akarnokd updated the answer withreduce
– Sarath Kn
Nov 19 at 14:15
add a comment |
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);
1
The key iszipWith
. You don't needscan
+lastElement
,reduce
suffices.
– akarnokd
Nov 19 at 14:10
@akarnokd updated the answer withreduce
– Sarath Kn
Nov 19 at 14:15
add a comment |
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);
1
The key iszipWith
. You don't needscan
+lastElement
,reduce
suffices.
– akarnokd
Nov 19 at 14:10
@akarnokd updated the answer withreduce
– Sarath Kn
Nov 19 at 14:15
add a comment |
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);
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);
edited Nov 19 at 14:20
answered Nov 19 at 14:09
Sarath Kn
1,500918
1,500918
1
The key iszipWith
. You don't needscan
+lastElement
,reduce
suffices.
– akarnokd
Nov 19 at 14:10
@akarnokd updated the answer withreduce
– Sarath Kn
Nov 19 at 14:15
add a comment |
1
The key iszipWith
. You don't needscan
+lastElement
,reduce
suffices.
– akarnokd
Nov 19 at 14:10
@akarnokd updated the answer withreduce
– 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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