优化代码 StringJoiner sj = new StringJoiner(","); for (Object[] row : filterdResult) { String v1 = "-Infinity".equals(String.valueOf(row[0])) ? "-(1/0)" : NumberUtil.toStr(BigDecimal.valueOf((Double) row[0])); String v2 = "Infinity".equals(String.valueOf(row[1])) ? "(1/0)" : NumberUtil.toStr(BigDecimal.valueOf((Double) row[1])); sj.add(String.format("and(%s <= `%s`.`%s`, %s > `%s`.`%s`), '%s'", v1, "$event_log", joinedColumnName, v2, "$event_log", joinedColumnName, v1 + PqlConst.VARIANT_DELIMETER + v2)); } return String.format(" multiIf(%s, null) ", sj.toString());
时间: 2024-02-02 21:02:36 浏览: 148
New-folder-(3).rar_H-infinity_h infinity_h infinity filter
可以使用 StringBuilder 来优化代码,因为每次添加字符串都会创建一个新的 String 对象,而 StringBuilder 可以在同一个对象上进行添加操作,从而减少对象的创建。另外,可以将 String.format 改为手动拼接字符串,也可以提高效率。代码示例如下:
```
StringBuilder sb = new StringBuilder();
for (Object[] row : filterdResult) {
String v1 = "-Infinity".equals(String.valueOf(row[0])) ? "-(1/0)" : NumberUtil.toStr(BigDecimal.valueOf((Double) row[0]));
String v2 = "Infinity".equals(String.valueOf(row[1])) ? "(1/0)" : NumberUtil.toStr(BigDecimal.valueOf((Double) row[1]));
sb.append("and(")
.append(v1)
.append(" <= `$event_log`.`")
.append(joinedColumnName)
.append("`, ")
.append(v2)
.append(" > `$event_log`.`")
.append(joinedColumnName)
.append("`), '")
.append(v1)
.append(PqlConst.VARIANT_DELIMETER)
.append(v2)
.append("', ");
}
if (sb.length() > 0) {
sb.delete(sb.length() - 2, sb.length());
}
return "multiIf(" + sb.toString() + ", null)";
```
阅读全文