gpt4 book ai didi

java - 压缩不同类型的多个可观察对象

转载 作者:行者123 更新时间:2023-11-29 04:45:25 24 4
gpt4 key购买 nike

如何使用 rx Java 一次压缩多个不同类型的 observable?

Observable<Action> actionStream = getAction();
Observable<UserInfo> ownerStream = getOwner();
Observable<List<UserInfo>> relatedUsersStream = getUsers();
Observable<Double> score = getScore();

//Question: how to merge all relusts at once nad call `getResult`?
getResult(action, owner, relatedUsers, score);

更新:找到解决方案。但问题是 getResult返回 Observable<Result>而不是 Result , 和 zip 方法不想编译。

Observable.<Action, UserInfo, List<UserInfo>, Double, Result>zip(
(action, owner, relatedUsers, score) -> getResult(action, owner, relatedUsers, score)
);

最佳答案

有 rxjava-joins 项目 http://mvnrepository.com/artifact/io.reactivex/rxjava-joins/0.22.0具有和/然后/何时功能。你可以做这样的事情:

  @Test
public void rxJoins() throws Exception {

Observable<Integer> ints = Observable.just(1, 2, 3);
Observable<String> strs = Observable.just("a", "b", "c");
Observable<Boolean> bools = Observable.just(false, false, true);

Plan0<Data> threeItems = JoinObservable.from(ints).and(strs).and(bools).then(Data::new);
JoinObservable.when(threeItems).toObservable().toBlocking().subscribe(System.out::println);
}

private static class Data {
private final int val1;
private final String val2;
private final boolean val3;

public Data(int val1, String val2, boolean val3) {
this.val1 = val1;
this.val2 = val2;
this.val3 = val3;
}

@Override
public String toString() {
return "Data{" +
"val1=" + val1 +
", val2='" + val2 + '\'' +
", val3=" + val3 +
'}';
}
}

更多信息在这里:http://www.introtorx.com/content/v1.0.10621.0/12_CombiningSequences.html

关于java - 压缩不同类型的多个可观察对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37387359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com