map集合优化
2019-09-29 本文已影响0人
花无重日人未红
通过map集合减少查询数据库次数
说明:将数据优先提取,减少访问数据库次数
示例:
if(paymentData!=null&&paymentData.size()>0) {
Map<String, List<PaymentBudget>> paymentBudgetMap = dao.getPaymentBudgetByYearAndNo(applyYear, budget.getCostCenterNo(), budget.getItemNo());
for(ContractPayment payment:paymentData) {
List<PaymentBudget>budgetData= paymentBudgetMap.get(payment.getPaymentId());
if(budgetData!=null&&budgetData.size()>0) {
for(PaymentBudget bud:budgetData) {
if(StringUtils.isBlank(bud.getBudgetNo())) {
double untaxAmount=bud.getUnTaxAmount();
useSubject=Arith.add(useSubject, untaxAmount);
}
}
}
}
}
public Map<String, List<PaymentBudget>> getPaymentBudgetByYearAndNo(int applyYear,String costCenterNo,String itemNo) {
String hql = " from " + PaymentBudget.class.getName() + " where year = ? and costCenterNo =? and itemNo= ? ";
List<PaymentBudget> list = find(hql,applyYear,costCenterNo,itemNo);
Map<String, List<PaymentBudget>> mapData = new HashMap<String, List<PaymentBudget>>();
if (list == null || list.size() == 0) {
return mapData;
}
for (PaymentBudget data : list) {
String paymentId = data.getPaymentId();
if (mapData.containsKey(paymentId)) {
List<PaymentBudget> listData = mapData.get(paymentId);
listData.add(data);
mapData.put(paymentId, listData);
}
else {
List<PaymentBudget> listData = new ArrayList<PaymentBudget>();
listData.add(data);
mapData.put(paymentId, listData);
}
}
return mapData;
}
反例:
if(paymentData!=null&&paymentData.size()>0) {
for(ContractPayment payment:paymentData) {
List<PaymentBudget>budgetData=dao.find(" from "+PaymentBudget.class.getName()+" where year = ? and costCenterNo =? and itemNo= ? and paymentId=? ",applyYear,budget.getCostCenterNo(),budget.getItemNo(),payment.getPaymentId());
if(budgetData!=null&&budgetData.size()>0) {
for(PaymentBudget bud:budgetData) {
if(StringUtils.isBlank(bud.getBudgetNo())) {
double untaxAmount=bud.getUnTaxAmount();
useSubject=Arith.add(useSubject, untaxAmount);
}
}
}
}
}
将嵌套for循环简化成单层for循环,降低时间复杂度
if(paymentData!=null&&paymentData.size()>0) {
Map<String, List<PaymentBudget>> paymentBudgetMap = dao.getPaymentBudgetByYearAndNo(applyYear, budget.getCostCenterNo(), budget.getItemNo());
List<PaymentBudget> list = new ArrayList<PaymentBudget>();
for(String paymentId : paymentBudgetMap.keySet()) {
List<PaymentBudget>budgetData= paymentBudgetMap.get(paymentId);
list.addAll(budgetData);
}
if(list!=null&&list.size()>0) {
for(PaymentBudget bud:list) {
if(StringUtils.isBlank(bud.getBudgetNo())) {
double untaxAmount=bud.getUnTaxAmount();
useSubject=Arith.add(useSubject, untaxAmount);
}
}
}
}
通过map集合减少某些循环嵌套次数
说明:可将外层for循环的内容存放在map集合中,降低时间复杂度
public void test1() {
List<Map<String, Object>> result = new ArrayList<>();
List<Map<String, Object>> result1 = new ArrayList<>();
List<Map<String, Object>> result2 = new ArrayList<>();
for (int i = 0; i < result2.size(); i++) {
Map<String, Object> timeNow = new HashMap<>();
timeNow.put("index_order", result2.get(i).get("index_order"));
for (int m = 0; m < result1.size(); m++) {
if (result2.get(i).get("index_id").equals(result1.get(m).get("index_id"))) {
timeNow.put("compare_value", Math.round(Double.parseDouble(result2.get(i).get("index_value") + "")/ Double.parseDouble(result1.get(m).get("index_value") + "") * 100)/ 100.00);
}
}
result.add(timeNow);
}
}
/**
* map集合优化嵌套循环
*/
public void test2() {
List<Map<String, Object>> result = new ArrayList<>();
List<Map<String, Object>> result1 = new ArrayList<>();
List<Map<String, Object>> result2 = new ArrayList<>();
Map<Object, Map> map = new HashMap<>();
for (int j = 0; j < result1.size(); j++) {
map.put(result1.get(j).get("index_id"), result1.get(j));
}
for (int i = 0; i < result2.size(); i++) {
Map<String, Object> timeNow = new HashMap<>();
timeNow.put("index_order", result2.get(i).get("index_order"));
if (map.get(result2.get(i).get("index_id")) != null) {
timeNow.put("compare_value",Math.round(Double.parseDouble(result2.get(i).get("index_value") + "")/ Double.parseDouble(map.get(result2.get(i).get("index_id")).get("index_value") + "")* 100) / 100.00);
}
result.add(timeNow);
}
}