gpt4 book ai didi

java - 使用哪个运算符来迭代数组元素

转载 作者:行者123 更新时间:2023-12-02 10:59:49 25 4
gpt4 key购买 nike

如下面的代码所示,我所拥有的可观察量将使用运算符 .map() 进行映射,然后我想使用 .filter 运算符。 .map() 运算符将返回一个数组,这个数组包含一些元素。如何将元素逐个传递给 .filter() 运算符。因为下面代码中的 .filter() 运算符接收到整个对象数组,所以我无法对每个元素进行过滤操作。

请告诉我应该在 .map 和 ,filter 之间使用哪个运算符,以便 .filter 运算符接收数组的元素而不是整个对象。

代码:

public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
Observable<String> animalsObservable = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

animalsObservable = Observable.just("Ant", "Bee", "Cat", "Dog", "Fox");
getAnimalsObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Function<List<String>, Object>() {
@Override
public Object apply(@NonNull List<String> strings) throws Exception {
List tempList = new ArrayList();
for (String string : strings) {
if (string.toLowerCase().startsWith("b")) {
tempList.add(string);
}
}
Log.i(TAG, "tempList: " + tempList);
String [] str = new String[tempList.size()];
tempList.toArray(str);
return str;
}
})
.filter(new Predicate<Object>() {
@Override
public boolean test(@NonNull Object o) throws Exception {
return false;
}
})
.subscribe((Consumer<? super Object>) getAnimalsObserver());
}

private Observable<List<String>> getAnimalsObservable() {
return Observable.fromArray(Arrays.asList(
new String[]{
"Ant", "Ape",
"Bat", "Bee", "Bear", "Butterfly",
"Cat", "Crab", "Cod",
"Dog", "Dove",
"Fox", "Frog"
}
));
}

private Observer<? super List<String>> getAnimalsObserver() {
return new Observer<List<String>>() {

@Override
public void onSubscribe(Disposable d) {
Log.i(TAG, "onSubscribe->d: " + d);
}

@Override
public void onNext(List<String> s) {
Log.i(TAG, "onNext->s: " + s);
}

@Override
public void onError(Throwable e) {
Log.i(TAG, "onError->e: " + e);
}

@Override
public void onComplete() {
Log.i(TAG, "onComplete");
}
};
}
}

最佳答案

map operator变换Observable<Something>进入Observable<SomethingElse>

在您的代码中,映射变换 Observable<List<String>>进入Observable<String[]>

如果你想过滤字符串,你需要 Observable<String>来自Observable<String[]>

这段代码应该可以正常工作,我使用了 flatmap然后toList使您的代码有效:

getAnimalsObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Function<List<String>, String[]>() { //map to String[]
@Override
public String[] apply(@NonNull List<String> strings) throws Exception {
List tempList = new ArrayList();
for (String string : strings) {
if (string.toLowerCase().startsWith("b")) {
tempList.add(string);
}
}
//Log.i(TAG, "tempList: " + tempList);
String[] str = new String[tempList.size()];
tempList.toArray(str);
return str;
}
})
.flatMap(new Function<String[], ObservableSource<String>>() { //flatmap to String
@Override
public ObservableSource<String> apply(String[] strings) throws Exception {
return Observable.fromArray(strings);
}
})
.filter(new Predicate<String>() {
@Override
public boolean test(String s) throws Exception {
return true; // filter with a condition
}
})
.toList()
.subscribe((Consumer<? super Object>) getAnimalsObserver());

但是,这种方法要好得多(我使用 java8 lambda 并从一开始就使用 Observable<String>):

    Observable.fromArray(new String[]{
"Ant", "Ape",
"Bat", "Bee", "Bear", "Butterfly",
"Cat", "Crab", "Cod",
"Dog", "Dove",
"Fox", "Frog"
})
//suppose that observable above came from another thread
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.filter(s -> s.toLowerCase().startsWith("b"))
.toList()
.subscribe((Consumer<? super Object>) getAnimalsObserver());

关于java - 使用哪个运算符来迭代数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51419299/

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