考慮以下類型:在RxJava什麼是減少表達
import io.reactivex.Observable;
import io.reactivex.observables.GroupedObservable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class TypeTest {
public static void main(String[] args) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
Observable<Long> source = Observable.interval(1,TimeUnit.MILLISECONDS).take(20);
Observable<GroupedObservable<String,Long>> sgb =
source.groupBy(x -> (x%2==0)?"Even":"Odd");
// I'd like to introduce a variable for source.reduce but I get a type error.
// Observable<Long> sreduce = source.reduce(new Long(0),(x,y) -> (x+y));
source.reduce(new Long(0),(x,y)->(x+y)).subscribe(x -> {
System.out.println(x);
latch.countDown();
});
latch.await();
}
}
我可以訂閱source.reduce就好像它是可觀察的,但我不能分配作爲其類型。我應該分配什麼類型?
OK之一,這只是似乎是在類型推斷的錯誤。如果我投它它運行良好。 –