出现的问题:
从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