工作中的各种小技巧

今天写文档,头大的一p。。。。于是看是想总结点有用的东西

第一:集合元素的删除

由于集合的下标问题,对其删除时需使用迭代器

如下:一个现有list集合,处理起来非常便利

List<ShopSimpleEpcInfo> list=checkService.getLoseSkuAndEpc(checkId);

Iterator<ShopSimpleEpcInfo> iter = list.iterator();
while (iter.hasNext()) {
ShopSimpleEpcInfo shopSimpleEpcInfo = iter.next();
if (shopSimpleEpcInfo.getWaitOnshelf()==0) {
iter.remove();
}
}



第二点;对于经常在返回的javabean中增加其他属性,又不想改变javabean的时候,可以使用一下方法,用string和json之间的互相转换,便利搞定

JSONArray arrResult = new JSONArray();
// 遍历最外层结果
for (AreaShopInfo area : list)
{
//转成jsonObject
JSONObject jArea = JSON.parseObject(JSON.toJSONString(area));
//创建信的jsonlist
JSONArray jArrList = new JSONArray();
//对第一层进行遍历
for (JHShopInfo shop : area.getList())
{
// 转 shop 为 json
JSONObject jShop = JSON.parseObject(JSON.toJSONString(shop));
     //将需要添加的属性进行json添加
jShop.put("orderCount", mapOrderCount.get(shop.getShopId()));
      // 将jshop重新驾到json集合中
jArrList.add(jShop);
}
  //恢复外层结构
jArea.put("list", jArrList);
jArea.put("orgParentName", area.getOrgParentName());
  返回
arrResult.add(jArea);
}





如有错误,请邮件zs253499660@sina.com,如有更好的方法,可以推荐
原文地址:https://www.cnblogs.com/senjiang/p/9627519.html