代码优化if (firstRun.equals(nowDate)) {//首次同步判断 if ("".equals(haveID) || haveID == null) { dataMap.put("linkedIn", staffMap.get("HR_USER_ID"));//中间表主表主键 dataMap.put("_status", "Insert"); } else { dataMap.put("id", QueryOne(sqlSession, "id", "iuap_apdoc_basedoc.bd_staff", "linkedin", String.valueOf(entry.getValue().get(0).get("HR_USER_ID"))));//主表ID /* 员工基本信息特征组---staffDefinesMap // TODO 以后优化为自动判断是否需要加入特征组,代码匹配自动化 */ // staffDefinesMap.put("id", UUID.randomUUID().toString()); dataMap.put("_status", "Update"); } } else { if ("".equals(haveID) || haveID == null) { dataMap.put("linkedIn", staffMap.get("HR_USER_ID"));//中间表主表主键 dataMap.put("_status", "Insert"); } else { if ("U".equals(staffMap.get("HR_STATUS_FLAG"))) { dataMap.put("id", QueryOne(sqlSession, "id", "iuap_apdoc_basedoc.bd_staff", "code", dataMap.get("code").toString()));//主表ID // staffDefinesMap.put("id", UUID.randomUUID().toString()); dataMap.put("_status", "Update"); } else { dataMap.put("linkedIn", staffMap.get("HR_USER_ID"));//中间表主表主键 dataMap.put("_status", "Insert"); } } }
时间: 2024-02-10 19:21:39 浏览: 137
这段代码中有几个地方可以进行优化:
1. 避免使用 == 和 equals 进行字符串比较,可以使用 Objects.equals() 方法进行比较,这样可以避免空指针异常。
2. 避免重复的代码,可以将相同的代码片段抽取出来,封装成方法或函数,以减少代码量和提高代码的可维护性。
3. 可以考虑使用三目运算符 (?:) 来代替 if-else 结构,这样可以更加简化代码。
4. 在需要进行多次字符串拼接时,不要使用字符串连接符 +,可以使用 StringBuilder 或 StringBuffer 类来进行字符串拼接,以提高性能。
相关问题
public static String wuxing(String gzYear, String gzMonth, String gzDay, String gzTime) { // 天干年 String gYear = gzYear.substring(0, 1); if ("甲".equals(gYear) || "乙".equals(gYear)) { gYear = "木"; } if ("丙".equals(gYear) || "丁".equals(gYear)) { gYear = "火"; } if ("戊".equals(gYear) || "己".equals(gYear)) { gYear = "土"; } if ("庚".equals(gYear) || "辛".equals(gYear)) { gYear = "金"; } if ("壬".equals(gYear) || "癸".equals(gYear)) { gYear = "水"; } // 天干月 String gMonth = gzMonth.substring(0, 1); if ("甲".equals(gMonth) || "乙".equals(gMonth)) { gMonth = "木"; } if ("丙".equals(gMonth) || "丁".equals(gMonth)) { gMonth = "火"; } if ("戊".equals(gMonth) || "己".equals(gMonth)) { gMonth = "土"; } if ("庚".equals(gMonth) || "辛".equals(gMonth)) { gMonth = "金"; } if ("壬".equals(gMonth) || "癸".equals(gMonth)) { gMonth = "水"; } // 天干日 String gDay = gzDay.substring(0, 1); if ("甲".equals(gDay) || "乙".equals(gDay)) { gDay = "木"; } if ("丙".equals(gDay) || "丁".equals(gDay)) { gDay = "火"; } if ("戊".equals(gDay) || "己".equals(gDay)) { gDay = "土"; } if ("庚".equals(gDay) || "辛".equals(gDay)) { gDay = "金"; } if ("壬".equals(gDay) || "癸".equals(gDay)) { gDay = "水"; } // 天干时 String gTime = gzTime.substring(0, 1); if ("甲".equals(gTime) || "乙".equals(gTime)) { gTime = "木"; } if ("丙".equals(gTime) || "丁".equals(gTime)) { gTime = "火"; } if ("戊".equals(gTime) || "己".equals(gTime)) { gTime = "土"; } if ("庚".equals(gTime) || "辛".equals(gTime)) { gTime = "金"; } if ("壬".equals(gTime) || "癸".equals(gTime)) { gTime = "水"; }
这段代码看起来是一个方法,名字叫做"wuxing",它接受四个参数:gzYear、gzMonth、gzDay、gzTime,都是String类型。在方法体内,它对这四个参数进行处理,将它们的首个字符与五行相对应,比如 "甲" 和 "乙" 对应 "木","丙" 和 "丁" 对应 "火",以此类推。最后,它将这四个处理后的字符串返回。我猜测这段代码可能是用于中医五行分析或者其他类似的领域。
if (button.equals("C")) { // clear txtResult.setText(""); num1 = 0; num2 = 0; op = ""; } else if (button.equals("+")) { // addition num1 = Double.parseDouble(txtResult.getText()); op = "+"; txtResult.setText(""); } else if (button.equals("-")) { // subtraction num1 = Double.parseDouble(txtResult.getText()); op = "-"; txtResult.setText(""); } else if (button.equals("*")) { // multiplication num1 = Double.parseDouble(txtResult.getText()); op = "*"; txtResult.setText(""); } else if (button.equals("/")) { // division num1 = Double.parseDouble(txtResult.getText()); op = "/"; txtResult.setText(""); } else if (button.equals("=")) { // equals num2 = Double.parseDouble(txtResult.getText()); if (op.equals("+")) { txtResult.setText(Double.toString(num1 + num2)); } else if (op.equals("-")) { txtResult.setText(Double.toString(num1 - num2)); } else if (op.equals("*")) { txtResult.setText(Double.toString(num1 * num2)); } else if (op.equals("/")) { txtResult.setText(Double.toString(num1 / num2)); } } else { // digit txtResult.setText(txtResult.getText() + button); }改用switch语句
可以将上述代码改写为使用 switch 语句的形式,代码如下所示:
```
switch (button) {
case "C":
txtResult.setText("");
num1 = 0;
num2 = 0;
op = "";
break;
case "+":
num1 = Double.parseDouble(txtResult.getText());
op = "+";
txtResult.setText("");
break;
case "-":
num1 = Double.parseDouble(txtResult.getText());
op = "-";
txtResult.setText("");
break;
case "*":
num1 = Double.parseDouble(txtResult.getText());
op = "*";
txtResult.setText("");
break;
case "/":
num1 = Double.parseDouble(txtResult.getText());
op = "/";
txtResult.setText("");
break;
case "=":
num2 = Double.parseDouble(txtResult.getText());
switch (op) {
case "+":
txtResult.setText(Double.toString(num1 + num2));
break;
case "-":
txtResult.setText(Double.toString(num1 - num2));
break;
case "*":
txtResult.setText(Double.toString(num1 * num2));
break;
case "/":
txtResult.setText(Double.toString(num1 / num2));
break;
}
break;
default:
txtResult.setText(txtResult.getText() + button);
break;
}
```
使用 switch 语句可以使代码更加简洁易读,并且能够避免出现多个 if 语句嵌套的情况。
阅读全文