日期:2014-05-19  浏览次数:20761 次

java,修改从缓存获取的集合,结果缓存的内容也修改了,求大侠指导
我直接贴代码了:

//获取缓存信息
public List<WeatherBoardInfo> getEhcacheInfo() {
//先取出缓存
Cache cache = cacheManager.getCache("com.linkcm.weather.enterprise.service.BoardEhcacheService");
Element e = cache.get("weatherBoardEhcache");
//根据key值取得缓存的值
List<WeatherBoardInfo> weatherBoardList = (List<WeatherBoardInfo>)e.getValue();
return weatherBoardList;
}

public EasyUiDataGrid selectAllBoardByEhcache(String instance_id) {
try {
List<WeatherBoardInfo> weatherBoardList = this.getEhcacheInfo();
List<WeatherBoardInfo> newList = new ArrayList<WeatherBoardInfo>(weatherBoardList);

//如果企业有实例就匹配
if (instance_id!=null && !instance_id.equals("")) {
//获取实例已有的板块
List<Map<String, Object>> list = this.homeJDBCDao.getContentByInstance(instance_id);

if (list.size()>0) {
for (WeatherBoardInfo w : newList) {
for (int i=0; i<list.size(); i++) {
Map<String, Object> map = list.get(i);
if (w.getId().equals(map.get("wb_id"))) {
w.setChecked(true);
break;
}
}
}
}
}

EasyUiDataGrid datagrid = MediaUtils.getEntityDataGrid(newList);
if (newList.size()<=0) {
return null;
}
return datagrid;
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
getLogger().error(e.getMessage(), e);
throw new ServiceException("获取缓存信息列表失败", e);
}
}

===============================
我后面都是对newList进行操作,但是当我第二次需要获取缓存内的信息时,发现拿到的内容直接就是这次修改后的内容了,我不想把缓存的也修改了,帮忙解决下,急需万分感谢。。

------解决方案--------------------
List<WeatherBoardInfo> newList = new ArrayList<WeatherBoardInfo>(weatherBoardList);

我是新手,个人理解!你这个问题是浅拷贝和深拷贝的问题!你还要把weatherBoardList中的WeatherBoardInfo对象复制一份才行!