简化了的代码
public List getData(Map map,List list){
Iterator it1=list.iterator();
while(it1.hasNext())
{
PortalStock portalStock1 = (PortalStock)it1.next();
String partno=portalStock1.getPartno();
PortalStock portalStock=new PortalStock();
PortalStock portalStock2=(PortalStock)map.get(portalStock1.getPartno());
if(null !=portalStock2)
{
BigDecimal qty = portalStock2.getTotalqty();
BigDecimal bottles = portalStock2.getTotalbottles();
BigDecimal weight = portalStock2.getTotalweight();
BigDecimal reciverqty = getzeroorself(portalStock1.getReciveqty());
BigDecimal reciverbottles = getzeroorself(portalStock1.getRecivebottles());
BigDecimal reciverweight = getzeroorself(portalStock1.getReciveweight());
BigDecimal deliverqty = getzeroorself(portalStock1.getDeliverqty());
BigDecimal deliverbottles = getzeroorself(portalStock1.getDeliverbottles());
BigDecimal deliverweight = getzeroorself(portalStock1.getDeliverweight());
portalStock1.setTotalqty(qty.add(reciverqty).subtract(deliverqty));
portalStock1.setTotalbottles(bottles.add(reciverbottles).subtract(deliverbottles));
portalStock1.setTotalweight(weight.add(reciverweight).subtract(deliverweight));
portalStock2.setTotalqty(qty.add(reciverqty).subtract(deliverqty));
portalStock2.setTotalbottles(bottles.add(reciverbottles).subtract(deliverbottles));
portalStock2.setTotalweight(weight.add(reciverweight).subtract(deliverweight));
}else{
BigDecimal qty = new BigDecimal(0);
BigDecimal bottles = new BigDecimal(0);
BigDecimal weight = new BigDecimal(0);
BigDecimal reciverqty = getzeroorself(portalStock1.getReciveqty());
BigDecimal reciverbottles = getzeroorself(portalStock1.getRecivebottles());
BigDecimal reciverweight = getzeroorself(portalStock1.getReciveweight());
BigDecimal deliverqty = getzeroorself(portalStock1.getDeliverqty());
BigDecimal deliverbottles = getzeroorself(portalStock1.getDeliverbottles());
BigDecimal deliverweight = getzeroorself(portalStock1.getDeliverweight());
portalStock1.setTotalqty(qty.add(reciverqty).subtract(deliverqty));
portalStock1.setTotalbottles(bottles.add(reciverbottles).subtract(deliverbottles));
portalStock1.setTotalweight(weight.add(reciverweight).subtract(deliverweight));
portalStock.setTotalqty(qty.add(reciverqty).subtract(deliverqty));
portalStock.setTotalbottles(bottles.add(reciverbottles).subtract(deliverbottles));
portalStock.setTotalweight(weight.add(reciverweight).subtract(deliverweight));
map.put(partno, portalStock);
}
}
return list;
}
public BigDecimal getzeroorself(BigDecimal b){
BigDecimal zero=new BigDecimal(0);
return zero=b==null?zero:b;
}
简化之前的代码
public List getData(Map map,List list){
Iterator it1=list.iterator();
while(it1.hasNext())
{
PortalStock portalStock1 = (PortalStock)it1.next();
Iterator it2=map.keySet().iterator();
Iterator it3=map.values().iterator();
boolean isHasSamePartno = false;
while(it2.hasNext()){
String Partno = (String)it2.next();
PortalStock portalStock2 = (PortalStock)it3.next();
if(portalStock1.getPartno().equals(Partno))
{
BigDecimal qty = portalStock2.getTotalqty();
BigDecimal bottles = portalStock2.getTotalbottles();
BigDecimal weight = portalStock2.getTotalweight();
BigDecimal reciverqty = portalStock1.getReciveqty();
BigDecimal reciverbottles = portalStock1.getRecivebottles();
BigDecimal reciverweight = portalStock1.getReciveweight();
BigDecimal deliverqty = portalStock1.getDeliverqty();
BigDecimal deliverbottles = portalStock1.getDeliverbottles();
BigDecimal deliverweight = portalStock1.getDeliverweight();
if("IN".equals(portalStock1.getStatus()))
{
portalStock1.setTotalqty(qty.add(reciverqty));
portalStock1.setTotalbottles(bottles.add(reciverbottles));
portalStock1.setTotalweight(weight.add(reciverweight));
}else if("OUT".equals(portalStock1.getStatus())){
portalStock1.setTotalqty(qty.subtract(deliverqty));
portalStock1.setTotalbottles(bottles.subtract(deliverbottles));
portalStock1.setTotalweight(weight.subtract(deliverweight));
}
isHasSamePartno = true;
break;
}
}
if(!isHasSamePartno){
BigDecimal zero = new BigDecimal(0);
String status = portalStock1.getStatus();
BigDecimal reciverqty = portalStock1.getReciveqty();
BigDecimal reciverbottles = portalStock1.getRecivebottles();
BigDecimal reciverweight = portalStock1.getReciveweight();
BigDecimal deliverqty = portalStock1.getDeliverqty();
BigDecimal deliverbottles = portalStock1.getDeliverbottles();
BigDecimal deliverweight = portalStock1.getDeliverweight();
if("IN".equals(status))
{
portalStock1.setTotalqty(zero.add(reciverqty));
portalStock1.setTotalbottles(zero.add(reciverbottles));
portalStock1.setTotalweight(zero.add(reciverweight));
}else if("OUT".equals(status)){
portalStock1.setTotalqty(zero.subtract(deliverqty));
portalStock1.setTotalbottles(zero.subtract(deliverbottles));
portalStock1.setTotalweight(zero.subtract(deliverweight));
}
map.put(portalStock1.getPartno(), portalStock1);
}
}
return list;
}
简化之前用的是两个嵌套循环,简化之后就只用了一个循环,性能有了很大提高,代码也变的简洁了