1 回答

TA貢獻1966條經(jīng)驗 獲得超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());
}
更新
第二次調(diào)用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())));
}
添加回答
舉報