原来的想法:
public Pagination findCollection(ShopMember shopMember, int pageNo, int pageSize) {
List<ProductItem> pis = new ArrayList<ProductItem>();
List<Long> pls = shopMember.getProductItems();
for(Long pl : pls) {
ProductItem p = this.findById(pl);
if( p == null) {
log.info("删除实效商品id:{}",pl);
//当时这里直接处理,删除
//原因与同步机制有关系吧,没有仔细研究
pls.remove(pl);
} else {
pis.add(p);
}
}
return new Pagination(pageNo, pageSize, pis.size(), pis);
}
修改后:主要是使用iterator接口,转换一遍
public Pagination findCollection(ShopMember shopMember, int pageNo, int pageSize) {
List<ProductItem> pis = new ArrayList<ProductItem>();
List<Long> pls = shopMember.getProductItems();
Iterator<Long> iterator = pls.iterator();
while(iterator.hasNext()) {
Long l = iterator.next();
ProductItem p = this.findById(l);
if(p == null) {
log.info("删除失效商品id:{}",l);
iterator.remove();
pls.remove(l);
} else {
pis.add(p);
}
}
return new Pagination(pageNo, pageSize, pis.size(), pis);
}