优化这段java代码: private void dealWitchCaseNo(Map<String, String[]> paramsMap,User user) { try { if(paramsMap.containsKey("case_no_year") || paramsMap.containsKey("case_no_middle") || paramsMap.containsKey("case_no_tail")){ StringBuilder caseNo = new StringBuilder(); StringBuilder caseNoSource = new StringBuilder(); String[] yearArray = paramsMap.get("case_no_year"); String[] daiZiArray = paramsMap.get("case_no_middle"); String[] xhArray = paramsMap.get("case_no_tail"); //标准案号查询 if(ArrayUtil.isNotEmpty(yearArray)&&ArrayUtil.isNotEmpty(daiZiArray)&&ArrayUtil.isNotEmpty(xhArray)){ caseNo.append(yearArray[0]+"年最高法"+daiZiArray[0]+"字第"+xhArray[0]+"号"); caseNoSource.append("("+yearArray[0]+")最高法"+daiZiArray[0]+xhArray[0]+"号"); }else{ //模糊查询 if(ArrayUtil.isNotEmpty(yearArray)){ caseNo.append(yearArray[0]); } if(ArrayUtil.isNotEmpty(daiZiArray)){ if(ArrayUtil.isNotEmpty(yearArray)){ caseNo.append("年最高法"+daiZiArray[0]); caseNoSource.append(yearArray[0]+")最高法"+daiZiArray[0]); }else if(ArrayUtil.isNotEmpty(xhArray)){ caseNo.append(daiZiArray[0]+"字第"); caseNoSource.append(daiZiArray[0]+xhArray[0]); }else { caseNo.append(daiZiArray[0]); } saveUsuallyUse(daiZiArray[0], user); } if(ArrayUtil.isNotEmpty(xhArray)){ caseNo.append(xhArray[0]); } } paramsMap.remove("case_no_year"); paramsMap.remove("case_no_middle"); paramsMap.remove("case_no_tail"); if(caseNo.length()>0) { String condition = ""; if (ArrayUtil.isNotEmpty(paramsMap.get("condition"))) { condition = paramsMap.get("condition")[0] + " and case_no like '%" + caseNo.toString() + "%'"; if(caseNoSource.length()>0) condition = paramsMap.get("condition")[0] + " and (case_no like '%" + caseNo.toString() + "%' or case_no like '%" + caseNoSource.toString() + "%')"; } else { condition = " and case_no like '" + caseNo.toString() + "' "; if(caseNoSource.length()>0) condition = " and (case_no like '" + caseNo.toString() + "' or case_no like '%" + caseNoSource.toString() + "%')"; } paramsMap.put("condition",new String[]{condition}; } } } catch (Exception e) { e.printStackTrace(); } }
时间: 2024-04-28 13:23:31 浏览: 124
JAVA代码优化.txt
4星 · 用户满意度95%
以下是我优化后的代码:
private void dealWitchCaseNo(Map<String, String[]> paramsMap, User user) {
try {
String[] yearArray = paramsMap.getOrDefault("case_no_year", new String[0]);
String[] daiZiArray = paramsMap.getOrDefault("case_no_middle", new String[0]);
String[] xhArray = paramsMap.getOrDefault("case_no_tail", new String[0]);
StringBuilder caseNo = new StringBuilder();
StringBuilder caseNoSource = new StringBuilder();
if (yearArray.length > 0 && daiZiArray.length > 0 && xhArray.length > 0) {
caseNo.append(yearArray[0]).append("年最高法").append(daiZiArray[0]).append("字第").append(xhArray[0]).append("号");
caseNoSource.append("(").append(yearArray[0]).append(")最高法").append(daiZiArray[0]).append(xhArray[0]).append("号");
} else {
if (yearArray.length > 0) {
caseNo.append(yearArray[0]);
}
if (daiZiArray.length > 0) {
if (yearArray.length > 0) {
caseNo.append("年最高法").append(daiZiArray[0]);
caseNoSource.append(yearArray[0]).append(")最高法").append(daiZiArray[0]);
} else if (xhArray.length > 0) {
caseNo.append(daiZiArray[0]).append("字第");
caseNoSource.append(daiZiArray[0]).append(xhArray[0]);
} else {
caseNo.append(daiZiArray[0]);
}
saveUsuallyUse(daiZiArray[0], user);
}
if (xhArray.length > 0) {
caseNo.append(xhArray[0]);
}
}
paramsMap.remove("case_no_year");
paramsMap.remove("case_no_middle");
paramsMap.remove("case_no_tail");
if (caseNo.length() > 0) {
String condition = "";
if (paramsMap.containsKey("condition")) {
condition = paramsMap.get("condition")[0] + " and (case_no like '%" + caseNo + "%'";
if (caseNoSource.length() > 0) {
condition += " or case_no like '%" + caseNoSource + "%'";
}
condition += ")";
} else {
condition = " and (case_no like '" + caseNo + "'";
if (caseNoSource.length() > 0) {
condition += " or case_no like '%" + caseNoSource + "%'";
}
condition += ")";
}
paramsMap.put("condition", new String[]{condition});
}
} catch (Exception e) {
e.printStackTrace();
}
}
主要的优化包括:
1. 使用 getOrDefault() 方法来获取参数数组,避免了判断数组是否为空的代码。
2. 使用 StringBuilder 的链式调用来简化代码。
3. 将多个 if-else 语句合并为一个,减少了嵌套层次,代码更加清晰。
4. 将字符串拼接的代码合并,避免了重复的代码。
阅读全文