无法解析 rxjava 2 中的方法 Observable.from
Cannot resolve method Observable.from in rxjava 2
在rxjava 1的Observable class中有一个from方法,在rxjava 2中找不到。如何在以下代码中替换rxjava 2中的from方法:
List<Integer> ints = new ArrayList<>();
for (int i=1; i<10; i++) {
ints.add(new Integer(i));
}
Observable.just(ints)
.flatMap(new Function<List<Integer>, Observable<Integer>>() {
@Override
public Observable<Integer> apply(List<Integer> ints) {
return Observable.from(ints);
}
})
您可以使用Observable.fromIterable(source)
Some operator overloads have been renamed with a postfix, such as
fromArray, fromIterable etc. The reason for this is that when the
library is compiled with Java 8, the javac often can't disambiguate
between functional interface types.
List<Integer> ints = new ArrayList<>();
for (int i=1; i<10; i++) {
ints.add(new Integer(i));
}
Observable.just(ints)
.flatMap(new Function<List<Integer>, Observable<Integer>>() {
@Override
public Observable<Integer> apply(List<Integer> ints) {
return Observable.fromIterable(ints);
}
})
您不需要使用 .just()
,因为您可以通过 fromIterable()
运算符直接从您的列表创建 Observable
。
Observable.fromIterable(ints)
我想有点晚了,但我只是想让人们知道,RxJava documentation 中与 from
运算符相关的 API 更改:
from
消歧为fromArray
、fromIterable
、fromFuture
如果您需要一个列表,那么您可以简单地使用 Java Stream
并从中创建一个 List
,然后将其直接传递给 Observable.fromIterable
方法。像这样:
Observable.fromIterable(IntStream.rangeClosed(lowerLimitInclusive,upperLimitInclusive)
.boxed()
.collect(Collectors.toList()));
或
Observable.fromIterable(Stream.iterate(lowerLimitInclusive, integer -> integer + 1)
.filter(integer -> integer < (upperLimit+1))
.limit((long(upperLimit+1)-long(lowerLimitInclusive))
.collect(Collectors.toList()));
在rxjava 1的Observable class中有一个from方法,在rxjava 2中找不到。如何在以下代码中替换rxjava 2中的from方法:
List<Integer> ints = new ArrayList<>();
for (int i=1; i<10; i++) {
ints.add(new Integer(i));
}
Observable.just(ints)
.flatMap(new Function<List<Integer>, Observable<Integer>>() {
@Override
public Observable<Integer> apply(List<Integer> ints) {
return Observable.from(ints);
}
})
您可以使用Observable.fromIterable(source)
Some operator overloads have been renamed with a postfix, such as fromArray, fromIterable etc. The reason for this is that when the library is compiled with Java 8, the javac often can't disambiguate between functional interface types.
List<Integer> ints = new ArrayList<>();
for (int i=1; i<10; i++) {
ints.add(new Integer(i));
}
Observable.just(ints)
.flatMap(new Function<List<Integer>, Observable<Integer>>() {
@Override
public Observable<Integer> apply(List<Integer> ints) {
return Observable.fromIterable(ints);
}
})
您不需要使用 .just()
,因为您可以通过 fromIterable()
运算符直接从您的列表创建 Observable
。
Observable.fromIterable(ints)
我想有点晚了,但我只是想让人们知道,RxJava documentation 中与 from
运算符相关的 API 更改:
from
消歧为fromArray
、fromIterable
、fromFuture
如果您需要一个列表,那么您可以简单地使用 Java Stream
并从中创建一个 List
,然后将其直接传递给 Observable.fromIterable
方法。像这样:
Observable.fromIterable(IntStream.rangeClosed(lowerLimitInclusive,upperLimitInclusive)
.boxed()
.collect(Collectors.toList()));
或
Observable.fromIterable(Stream.iterate(lowerLimitInclusive, integer -> integer + 1)
.filter(integer -> integer < (upperLimit+1))
.limit((long(upperLimit+1)-long(lowerLimitInclusive))
.collect(Collectors.toList()));