我有一个返回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));
}
答案 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));