优化以下代码 JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("logTimeJson")); Map<String, Long> bbb = (Map<String, Long>) jsonArray.stream() .filter(map -> ((Map<String,Long>)map).get("vehicleTime") <= occurredOn.getTime()) .min(Comparator.comparingLong(map1 -> occurredOn.getTime() - ((Map<String,Long>)map1).get("vehicleTime"))) .orElse(null); Long time = occurredOn.getTime()-(bbb.get("vehicleTime")-bbb.get("thirdTime"));
时间: 2024-03-15 09:47:19 浏览: 163
安卓下通常采用以下几种方式解析json数据汇编.pdf
可以考虑以下优化:
1. 避免频繁类型转换:可以在读取 jsonArray 时将其转换为 List<Map<String, Long>> 类型,避免多次类型转换。
2. 避免重复计算:可以将 occurredOn.getTime() 提前计算并保存,避免重复计算。
优化后的代码如下:
```
List<Map<String, Long>> list = JSONArray.parseArray(jsonObject.getString("logTimeJson"), Map.class);
long occurredOnTime = occurredOn.getTime();
Map<String, Long> bbb = list.stream()
.filter(map -> map.get("vehicleTime") <= occurredOnTime)
.min(Comparator.comparingLong(map1 -> occurredOnTime - map1.get("vehicleTime")))
.orElse(null);
if (bbb != null) {
Long time = occurredOnTime - (bbb.get("vehicleTime") - bbb.get("thirdTime"));
}
```
阅读全文