public String bxJsonTobin(List<LightGroupReq> lightGroupReqs, Integer binHashCode,List<String> lightGroupList) { LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_X); ArrayList<String> list = new ArrayList<>(); ArrayList<String> writeBinList = new ArrayList<>(); Map<Integer, Frames> portMap = new HashMap<>(); List<Frames> frameList = new ArrayList<>(); for (int i = 1; i <= 78; i++) { frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } groupingFrames.put((long) groupingFrames.size(), frameList); long timeStamp = 0; for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) { List<Frames> framesList = entry.getValue(); int[] temp = new int[79]; //全部初始化为-1 Arrays.fill(temp, -1); for (int i = 0; i < framesList.size(); i++) { temp[framesList.get(i).getGroupIndex()] = 1; } // 补充FF 数据帧 for (int i = 1; i < temp.length; i++) { if (temp[i] == -1) { portMap.put(i, new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } } framesList.addAll(portMap.values()); framesList = framesList.stream().sorted(Comparator.comparing(Frames::getGroupIndex)).collect(Collectors.toList()); portMap.clear(); if(timeStamp>7){ timeStamp = 0; } for (int j = 0; j < framesList.size(); j++) { list.add(BinaryFileUtils.bxFramesToHex(framesList.get(j),timeStamp,writeBinList)); } timeStamp++; } return fileService.uploadBin(list, binHashCode); }优化这段代码
时间: 2024-02-14 13:13:45 浏览: 104
以下是对代码的优化建议:
1. 减少不必要的内存占用和时间复杂度
- 在循环中新建了一个长度为78的List对象,然后再添加到groupingFrames中,这样做会导致不必要的内存占用和时间复杂度,可以直接在groupingFrames中添加新的List对象;
- 在循环中对temp数组进行了全部初始化为-1的操作,可以将这个操作提到循环外,只进行一次初始化操作,这样可以减少不必要的时间复杂度。
2. 避免多次对同一个对象进行操作
- 在循环中对framesList进行了多次操作,包括添加、排序和清空,可以将这些操作提到循环外,只进行一次操作,这样可以避免多次对同一个对象进行操作。
3. 减少重复代码
- 在循环中多次使用了Frames类的构造方法,可以将这个构造方法封装成一个方法,避免重复代码。
综上所述,以下是优化后的代码:
```
public String bxJsonTobin(List<LightGroupReq> lightGroupReqs, Integer binHashCode, List<String> lightGroupList) {
LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs, VehicleConstants.VEHICLE_X);
ArrayList<String> list = new ArrayList<>();
ArrayList<String> writeBinList = new ArrayList<>();
Map<Integer, Frames> portMap = new HashMap<>();
List<Frames> frameList = new ArrayList<>();
for (int i = 1; i <= 78; i++) {
frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2));
}
groupingFrames.put((long) groupingFrames.size(), frameList);
long timeStamp = 0;
int[] temp = new int[79];
Arrays.fill(temp, -1);
for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) {
List<Frames> framesList = entry.getValue();
for (int i = 0; i < framesList.size(); i++) {
temp[framesList.get(i).getGroupIndex()] = 1;
}
for (int i = 1; i < temp.length; i++) {
if (temp[i] == -1) {
portMap.put(i, new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2));
}
}
framesList.addAll(portMap.values());
portMap.clear();
framesList = framesList.stream().sorted(Comparator.comparing(Frames::getGroupIndex)).collect(Collectors.toList());
if (timeStamp > 7) {
timeStamp = 0;
}
for (int j = 0; j < framesList.size(); j++) {
list.add(BinaryFileUtils.bxFramesToHex(framesList.get(j), timeStamp, writeBinList));
}
timeStamp++;
Arrays.fill(temp, -1);
}
return fileService.uploadBin(list, binHashCode);
}
private Frames createFrames(int groupIndex) {
return new Frames(7, 63, 63, 2550, 15, 15, 0, groupIndex, 0, 2);
}
```
阅读全文