FlatMap是一个可迭代的,并将其组合回可迭代

时间:2018-04-24 11:10:38

标签: java android rx-java rx-java2

我有一个返回Single的API。此Single包含值列表,我们说String值。当我调用此对象时,我得到Single并且必须从中过滤掉一些值并返回另一个Single。我试图在这个简化的测试中实现这样的目标:

@Test
public void filterTest() {

    List<String> sourceList = Arrays.asList("email", "phone", "smoke", "email", "phone", "fax", "email");

    Single.just(sourceList)
            .toObservable()
            .flatMap(source -> {
                return Observable.from(source);
            })
            .filter(source -> !source.equals("email"))
            .groupBy(/* criteria? */)
            //how to extract single list from groupBy or 
            //is there another opposite function for flatMap?
            .toSingle()
            .subscribe(s -> System.out.println(s));
}

1 个答案:

答案 0 :(得分:1)

试试这个:

Single.just(sourceList)
        .flattenAsObservable(source -> source)
        .filter(source -> !source.equals("email"))
        .toList()
        .subscribe(s -> System.out.println(s));

 Observable.fromIterable(sourceList)
        .filter(source -> !source.equals("email"))
        .toList()
        .subscribe(s -> System.out.println(s));