两个方法都是做盘点数据与库存数据的比较,method2为修改后方法,两个方法结构调整的意义,是否带来结构上的优化和性能上的提升?
if(){}else{}的嵌套使用相较于if(){},if(){}结构上的优劣又体现在哪里?
Method1:
/** * 一次盘点与库存比较 * * @param list盘点数据集 * @param checkId * @return listReault差异结果集 */ private List<StorageCheckDiffReaultDetail> compareFirstDetail2(int checkId){ StorageCheck storageCheck = storageCheckDao.findById(checkId); if(storageCheck==null){ throw new RuntimeException("盘点id不存在!!!"); } Integer positionId=storageCheck.getPositionId(); // 比较结果 List<StorageCheckDiffReaultDetail> listResault = new ArrayList<StorageCheckDiffReaultDetail>(); // 把库存封装成StorageCheckSnapDataDetail对象列表 List<StorageCheckSnapDataDetail> listPosDetails = processGetListPosDetail(positionId); // 把快照数据封装成StorageCheckSnapDataDetail对象列表 List<StorageCheckSnapDataDetail> listSnapDetails = processFindListSnapDetail(checkId); //排序 Collections.sort(listPosDetails); Collections.sort(listSnapDetails); int j=0; int i=0; for (;;) { if(i>=listSnapDetails.size()&&j>=listPosDetails.size()){//不存在数据结束循环 break; } Integer posSkuId=listPosDetails.get(j).getSkuId(); Integer posNum=listPosDetails.get(j).getSnapNum(); if(i>=listSnapDetails.size()&&j<listPosDetails.size()){//只剩库存数据 StorageCheckDiffReaultDetail diffData= new StorageCheckDiffReaultDetail(posSkuId, 0, posNum); listResault.add(diffData); j++; continue; } Integer snapSkuId=listSnapDetails.get(i).getSkuId(); Integer snapNum=listSnapDetails.get(i).getSnapNum(); if(j>=listPosDetails.size()&&i<listSnapDetails.size()){//只剩真实盘点数据 StorageCheckDiffReaultDetail diffData= new StorageCheckDiffReaultDetail(snapSkuId, snapNum, 0); listResault.add(diffData); i++; continue; } if(snapSkuId.equals(posSkuId)){//skuId相同, if(snapNum.equals(posNum)){//数量相同 }else{//数量不同 StorageCheckDiffReaultDetail diffData= new StorageCheckDiffReaultDetail(snapSkuId, snapNum, posNum); listResault.add(diffData); } i++; j++; continue; }else{//skuId不同 if(snapSkuId<posSkuId){//真实数据存在,库存不存在 StorageCheckDiffReaultDetail diffData= new StorageCheckDiffReaultDetail(snapSkuId, snapNum, 0); listResault.add(diffData); i++; continue; }else{//真实数据不存在,库存存在 StorageCheckDiffReaultDetail diffData= new StorageCheckDiffReaultDetail(posSkuId, 0, posNum); listResault.add(diffData); j++; continue; } } } return listResault; }
Method2:
/** * 一次盘点与库存比较 * * @param list盘点数据集 * @param checkId * @return listReault差异结果集 */ private List<StorageCheckDiffReaultDetail> compareFirstDetail(int checkId){ StorageCheck storageCheck = storageCheckDao.findById(checkId); if(storageCheck==null){ throw new RuntimeException("盘点id不存在!!!"); } Integer positionId=storageCheck.getPositionId(); // 比较结果 List<StorageCheckDiffReaultDetail> listResault = new ArrayList<StorageCheckDiffReaultDetail>(); // 把库存封装成StorageCheckSnapDataDetail对象列表 List<StorageCheckSnapDataDetail> listPosDetails = processGetListPosDetail(positionId); // 把快照数据封装成StorageCheckSnapDataDetail对象列表 List<StorageCheckSnapDataDetail> listSnapDetails = processFindListSnapDetail(checkId); //排序 Collections.sort(listPosDetails); Collections.sort(listSnapDetails); int j=0; int i=0; while(i<listSnapDetails.size()&&j<listPosDetails.size()) { Integer posSkuId=listPosDetails.get(j).getSkuId(); Integer posNum=listPosDetails.get(j).getSnapNum(); Integer snapSkuId=listSnapDetails.get(i).getSkuId(); Integer snapNum=listSnapDetails.get(i).getSnapNum(); // skuId和数量都相同 if(snapSkuId.equals(posSkuId) && snapNum.equals(posNum)){ i++; j++; continue; } // skuId相同但数量不同 if (snapSkuId.equals(posSkuId) && !snapNum.equals(posNum)) { listResault.add(new StorageCheckDiffReaultDetail(snapSkuId, snapNum, posNum)); i++; j++; continue; } // skuId不同:真实数据存在,库存不存在 if (snapSkuId < posSkuId) {// 真实数据存在,库存不存在 listResault.add(new StorageCheckDiffReaultDetail(snapSkuId, snapNum, 0)); i++; continue; } // skuId不同:真实数据不存在,库存存在 listResault.add(new StorageCheckDiffReaultDetail(posSkuId, 0, posNum)); j++; continue; }; List<StorageCheckSnapDataDetail> listSnap; // 如果快照有剩余记录,全部添加到listResults中 if(i<listSnapDetails.size()) { listSnap= listSnapDetails.subList(i, listSnapDetails.size()); for (StorageCheckSnapDataDetail dataDetail : listSnap) { listResault.add(new StorageCheckDiffReaultDetail(dataDetail.getSkuId(), dataDetail.getSnapNum(), 0)); } } // 如果实际库存有剩余记录,全部添加到listResults中 if(j<listPosDetails.size()) { //listResault.addAll(restCheckList(listPosDetails, j)); listSnap= listPosDetails.subList(i, listPosDetails.size()); for (StorageCheckSnapDataDetail dataDetail : listSnap) { listResault.add(new StorageCheckDiffReaultDetail(dataDetail.getSkuId(), 0,dataDetail.getSnapNum())); } } return listResault; }
时间: 2024-10-08 10:29:50