两个集合的数据有上万条,导出内存溢出,请将下面的代码优化 for (int i = 0; i <ls.size() ; i++) { Map<String,Object> sb1=new HashMap<>(); Map<String,Object> sb2=new HashMap<>(); boolean sbboolean1=false; boolean sbboolean2=false; for (int j = 0; j <list2.size() ; j++) { if(StringUtils.isNotEmpty(ls.get(i).getSalesorder())&&StringUtils.isNotEmpty(list2.get(j).getSalesorder())){ if(ls.get(i).getSalesorder().trim().equals(list2.get(j).getSalesorder().trim())){ if(StringUtils.isNotEmpty(list2.get(j).getDeliveryresponse())){ sbboolean1=true; sb1.put(list2.get(j).getDeliveryresponse().trim(),null); } if(StringUtils.isNotEmpty(list2.get(j).getOrderdate())){ sbboolean2=true; sb2.put(list2.get(j).getOrderdate().trim(),null); } } } } if(sbboolean1){ StringBuilder sbdate1=new StringBuilder(); for (String s:sb1.keySet()) { sbdate1.append(s+" "); } ls.get(i).setLowmaterialstatusb(sbdate1.toString()); } if(sbboolean2){ StringBuilder sbdate2=new StringBuilder(); for (String s:sb2.keySet()) { sbdate2.append(s+" "); } ls.get(i).setLowmaterialstatus(sbdate2.toString()); } }
时间: 2024-02-14 10:09:57 浏览: 122
这段代码可以进行以下优化:
1. 避免在循环中频繁调用 `ls.get(i)` 和 `list2.get(j)` 方法,可以先将它们的值存储在变量中:
```
for (int i = 0; i < ls.size(); i++) {
Map<String, Object> sb1 = new HashMap<>();
Map<String, Object> sb2 = new HashMap<>();
boolean sbboolean1 = false;
boolean sbboolean2 = false;
Object salesorder1 = ls.get(i).getSalesorder();
if (StringUtils.isNotEmpty(salesorder1)) {
String salesorder1Trim = salesorder1.toString().trim();
for (int j = 0; j < list2.size(); j++) {
Object salesorder2 = list2.get(j).getSalesorder();
if (StringUtils.isNotEmpty(salesorder2)) {
String salesorder2Trim = salesorder2.toString().trim();
if (salesorder1Trim.equals(salesorder2Trim)) {
Object deliveryresponse = list2.get(j).getDeliveryresponse();
if (StringUtils.isNotEmpty(deliveryresponse)) {
sbboolean1 = true;
sb1.put(deliveryresponse.toString().trim(), null);
}
Object orderdate = list2.get(j).getOrderdate();
if (StringUtils.isNotEmpty(orderdate)) {
sbboolean2 = true;
sb2.put(orderdate.toString().trim(), null);
}
}
}
}
}
// ...
}
```
2. 如果 `sb1` 和 `sb2` 中存储的值只需要是键的集合,可以使用 `HashSet` 代替 `HashMap`,避免在循环中频繁创建和销毁 value 对象:
```
for (int i = 0; i < ls.size(); i++) {
Set<String> sb1 = new HashSet<>();
Set<String> sb2 = new HashSet<>();
boolean sbboolean1 = false;
boolean sbboolean2 = false;
Object salesorder1 = ls.get(i).getSalesorder();
if (StringUtils.isNotEmpty(salesorder1)) {
String salesorder1Trim = salesorder1.toString().trim();
for (int j = 0; j < list2.size(); j++) {
Object salesorder2 = list2.get(j).getSalesorder();
if (StringUtils.isNotEmpty(salesorder2)) {
String salesorder2Trim = salesorder2.toString().trim();
if (salesorder1Trim.equals(salesorder2Trim)) {
Object deliveryresponse = list2.get(j).getDeliveryresponse();
if (StringUtils.isNotEmpty(deliveryresponse)) {
sbboolean1 = true;
sb1.add(deliveryresponse.toString().trim());
}
Object orderdate = list2.get(j).getOrderdate();
if (StringUtils.isNotEmpty(orderdate)) {
sbboolean2 = true;
sb2.add(orderdate.toString().trim());
}
}
}
}
}
// ...
}
```
3. 如果需要把 `sb1` 和 `sb2` 中的值拼接成字符串并赋值给对象的属性,可以直接使用 `String.join()` 方法,避免使用 `StringBuilder`:
```
for (int i = 0; i < ls.size(); i++) {
Set<String> sb1 = new HashSet<>();
Set<String> sb2 = new HashSet<>();
boolean sbboolean1 = false;
boolean sbboolean2 = false;
Object salesorder1 = ls.get(i).getSalesorder();
if (StringUtils.isNotEmpty(salesorder1)) {
String salesorder1Trim = salesorder1.toString().trim();
for (int j = 0; j < list2.size(); j++) {
Object salesorder2 = list2.get(j).getSalesorder();
if (StringUtils.isNotEmpty(salesorder2)) {
String salesorder2Trim = salesorder2.toString().trim();
if (salesorder1Trim.equals(salesorder2Trim)) {
Object deliveryresponse = list2.get(j).getDeliveryresponse();
if (StringUtils.isNotEmpty(deliveryresponse)) {
sbboolean1 = true;
sb1.add(deliveryresponse.toString().trim());
}
Object orderdate = list2.get(j).getOrderdate();
if (StringUtils.isNotEmpty(orderdate)) {
sbboolean2 = true;
sb2.add(orderdate.toString().trim());
}
}
}
}
}
if (sbboolean1) {
ls.get(i).setLowmaterialstatusb(String.join(" ", sb1));
}
if (sbboolean2) {
ls.get(i).setLowmaterialstatus(String.join(" ", sb2));
}
}
```
阅读全文