1 回答
TA贡献1966条经验 获得超4个赞
你可以这样做,
private Observable<List<Result<Account, IError>>> filterAccounts(Context context, List<Account> accounts){
return accountDAL.getByIds(context, accounts
.stream()
.map(a -> Long.valueOf(a.getAccountId()))
.collect(Collectors.toList()))
.map(a ->
a.stream()
.collect(Collectors.toMap(a -> a.getId(), Function.identity())) // map ==> {id = Account}
).map(seMap ->
accountDAL.save(context, accounts.stream()
.filter(a -> seMap.get(Long.valueOf(a.getAccountId())) == null)
.collect(Collectors.toList())).first());
}
更新
第二次调用save返回一个Observable<?>(只是一个假设),当它被包装在一个map运算符中时,它返回Observable<Observable<?>>。但是你需要的返回值是Observable<?>. 所以,你需要拼合Observable<Observable<?>>到Observable<?>哪里,这就是flatMap被使用。如果需要,这里是更新的答案。
private Observable<List<Result<Account, IError>>> filterAccounts(Context context, List<Account> accounts) {
return accountDAL
.getByIds(context,
accounts.stream().map(a -> Long.valueOf(a.getAccountId())).collect(Collectors.toList()))
.map(ar -> ar.stream().collect(Collectors.toMap(Account::getAccountId, Function.identity())) // map ==>
// {id =
// Account}
).flatMap(seMap -> accountDAL.save(context, accounts.stream()
.filter(a -> seMap.get(Long.valueOf(a.getAccountId())) == null).collect(Collectors.toList())));
}
添加回答
举报