2

我有一个对象 A 的列表,并且在每个 AI 中都有一个对象 B 的列表。

这是我想做的:

List<A>使用 RxJava 循环时,我希望能够循环内部列表并List<B>在最后对 and 进行一些过滤,我想将过滤List<B>后的List<A>.

在不破坏链条的情况下可以进行此操作吗?

像这个例子:

class A {
        int mAId;
        List<B> mBList;
    }

    class B{
        int mId;
    }

    public void loop(List<A> list){
        Observable.fromIterable(list)
                .flatMapIterable(objs-> objs.mBList)
                .filter("filtering each b")
                .toList()
                .map("add back the list of b to the right a")
                .subscribe()
    }
4

4 回答 4

0

我的意见:

    public void loop(List<A> list){
     List<FromObject> innerList;
     list.forEach(e->{
      innerList=e.getInnerList();
      innerList.forEach(in->{
      <apply your filter here>
      <for example>
       if(in.getNumber()>0)
        innerList.remove(in);
      }
     }
    }

希望它会有所帮助

于 2018-05-28T06:14:58.283 回答
0

是的,您可以通过嵌套反应流来实现它:

List<List<Integer>> listOfLists = Arrays.asList(
        Arrays.asList(0, 1, 2, 3, 4, 5),
        Arrays.asList(6, 7, 8, 9, 10, 11),
        Arrays.asList(12, 13, 14, 15, 16, 17),
        Arrays.asList(18, 19, 20, 21, 22, 23)
);

Observable.fromIterable(listOfLists)
        .flatMap((innerList) -> {
            return Observable.fromIterable(innerList)
                    .filter((value) -> value % 2 == 0)
                    .toList()
                    .toObservable();
        })
        .toList()
        .subscribe(listOfListsFiltered -> {
            for (List<Integer> innerList : listOfListsFiltered) {
                for (Integer value : innerList) {
                    System.out.print(value + ", ");
                }

                System.out.println();
            }
        });
}
于 2018-05-28T07:01:07.743 回答
0

我终于找到了这个问题的答案:

class A(val filterable: Boolean = random.nextBoolean())

class B(val name: String = random.nextInt().toString(), val listOfA: List<A> = listOf(A(), A(), A(), A(), A(), A()))

@Test
fun rxTest() {
    Observable.fromIterable(listOf(B(), B(), B(), B(), B(), B(), B()))
            .compose {
                it.publish<B> { publish ->
                    val filteredList = publish.flatMapIterable { it.listOfA }
                            .filter {
                                it.filterable
                            }
                            .toList()
                            .toObservable()
                    Observable.zip<B, List<A>, B>(publish, filteredList, BiFunction<B, List<A>, B> { b, listOfA ->
                        B(b.name, listOfA)
                    })
                }
            }
}
于 2018-09-24T15:13:47.767 回答
-1

为未来的读者或更好的答案提供灵感。还有另一种方法可以做到这一点

public static class A {
        int id;
        List<B> mBList;
    }

    public static class B {
        int id;
    }

    public void test() {

        Observable.just(Arrays.asList(new A(), new A(), new A()))
                .flatMapIterable(a -> a)
                .map(a -> {
                    a.mBList = Observable.fromIterable(a.mBList)
                            .distinct()
                            .toList()
                            .blockingGet();
                    return a;
                });
    }
于 2018-06-04T11:15:48.820 回答