出現的問題:
從mongodb中查詢出的數據使用List接收之后,再往這個list中追加數據時,拋出java.lang.UnsupportedOperationException異常
AggregationResults<StatisticHospitalListOfFeeBillVo> aggregationResults = mongoTemplate.aggregate(aggregation, StatisticHospitalListOfFeeBillVo.class);
List<StatisticHospitalListOfFeeBillVo> resultList = aggregationResults.getMappedResults();
List<StatisticHospitalListOfFeeBillVo> list = Lists.newArrayList();
if (resultList != null) {
resultList.stream().forEach(result -> {result.setHospitalName(hospitalNameMap.get(result.getHid()));hospitalNameMap.remove(result.getHid());});
list.addAll(resultList);
}
for (Long hid:hospitalNameMap.keySet()) {
StatisticHospitalListOfFeeBillVo vo = new StatisticHospitalListOfFeeBillVo();
vo.setHospitalName(hospitalNameMap.get(hid));
vo.setHid(hid);
// 在此處拋出異常
list.add(vo);
}
原因:
在AggregationResults的源碼中,查詢出來的結果集使用了Collections.unmodifiableList進行包裝,所以返回的list是不能新增或刪除的,源碼如下

返回了一個不可修改的list

