早上好,
我有一个休息api。在那里我可以从给定的id中获取名称:
@GET("warehouse/{wid}/name")
Observable<Response<String>> getWarehouseName(@Path(WAREHOUSE_ID) String wid);
我实现了这个方法,在我的客户端获取名称:
protected Observable<String> getNameObservable(final String wid) {
if (wid == null) {
throw new IllegalArgumentException("wid is null");
}
return warehousesService.getWarehouseName(wid).subscribeOn(Schedulers.newThread())
.flatMap(response -> {
try {
CommonResponseChecks.checkResponse(response);
return Observable.just(response.body());
} catch (NotFoundException e) {
return Observable.error(e);
}
});
}
现在我想用rxjava实现一个方法,该方法将id列表作为参数并返回一个映射public Map<String, String> getNames(Collection<String> wids)
我有这些方法来获取名称列表:
public Maybe<List<String>> getNames(Collection<String> warehouseIds) {
return Observable.fromIterable(warehouseIds).subscribeOn(Schedulers.newThread())
.flatMapMaybe(wid -> getName(wid)).toList().toMaybe();
}
但是如何将其转换为地图?
抱歉我的英语不好;)最好的问候
答案 0 :(得分:0)
也许你正在寻找类似的东西:
public Map<String, String> getNames(Collection<String> wids) {
return Observable.fromIterable(warehouseIds)
.subscribeOn(Schedulers.newThread())
.collect(Collectors.toMap(wid-> wid, wid -> getName(wid)));
}