private Map<String, Integer> pmCleanParas 对其进行初始化
时间: 2023-07-16 16:16:05 浏览: 42
在Java中,可以使用以下代码对Map进行初始化:
```
Map<String, Integer> pmCleanParas = new HashMap<String, Integer>();
```
这将创建一个空的HashMap,其中键和值的类型分别为String和Integer。要向Map添加键值对,可以使用put()方法,例如:
```
pmCleanParas.put("key1", 1);
pmCleanParas.put("key2", 2);
pmCleanParas.put("key3", 3);
```
这将向pmCleanParas中添加三个键值对,分别为("key1", 1),("key2", 2)和("key3", 3)。您可以按照需要添加更多的键值对。
相关问题
调用“@Resource private ApplicationServicePlatformClientProxy applicationServicePlatformClientProxy;”获取“projectName"修改下面代码:@Override public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) { List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId); if(CollectionUtil.isEmpty(taskTaskList)){ return Collections.emptyList(); } List<ProjectCountVO> result = new ArrayList<>(); Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId)); for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) { projectId = projectTaskEntry.getKey(); ProjectCountVO projectCountVO = new ProjectCountVO(); projectCountVO.setId(projectId); //查询项目名称 projectName = applicationServicePlatformClientProxy.get String projectName = Optional.ofNullable(projectTaskMap.get(projectId)).map(taskList -> taskList.get(0).getName()).orElse(""); projectCountVO.setName(projectName); List<TaskTask> taskList = projectTaskEntry.getValue(); projectCountVO.setTaskCount(taskList.size()); Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0 : taskList.stream().collect(Collectors.summingInt(task-> Optional.ofNullable(task.getProblemNum()).orElse(0))); projectCountVO.setProblemCount(problemCount); result.add(projectCountVO); } return result; }
请注意,你在代码中使用了变量“projectName”,但没有对其进行初始化或声明。我假设你想要从应用服务平台获取项目名称,因此你需要调用应用服务平台客户端代理的相应方法,以获取项目名称。请修改以下代码:
@Override
public List<ProjectCountVO> projectCount(String beginTime, String endTime, Integer forceType, String projectId) {
List<TaskTask> taskTaskList = this.listStatisticsTask(beginTime, endTime, forceType, projectId);
if (CollectionUtil.isEmpty(taskTaskList)) {
return Collections.emptyList();
}
List<ProjectCountVO> result = new ArrayList<>();
Map<String, List<TaskTask>> projectTaskMap = taskTaskList.stream().collect(Collectors.groupingBy(TaskTask::getProjectId));
for (Map.Entry<String, List<TaskTask>> projectTaskEntry : projectTaskMap.entrySet()) {
projectId = projectTaskEntry.getKey();
ProjectCountVO projectCountVO = new ProjectCountVO();
projectCountVO.setId(projectId);
// 查询项目名称
String projectName = applicationServicePlatformClientProxy.getProjectName(projectId);
projectCountVO.setName(projectName);
List<TaskTask> taskList = projectTaskEntry.getValue();
projectCountVO.setTaskCount(taskList.size());
Integer problemCount = CollectionUtil.isEmpty(taskList) ? 0
: taskList.stream().collect(Collectors.summingInt(task -> Optional.ofNullable(task.getProblemNum()).orElse(0)));
projectCountVO.setProblemCount(problemCount);
result.add(projectCountVO);
}
return result;
}
private List<String> efJsonToBinList(List<LightGroupReq> lightGroupReqs){ LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_009); ArrayList<String> list = new ArrayList<>(); Map<Integer, Frames> portMap = new HashMap<>(); List<Frames> frameList = new ArrayList<>(); for (int i = 1; i <= 48; i++) { frameList.add(new Frames(255, 255, 2550, 2550, 255, 255, 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[49]; //全部初始化为-1 Arrays.fill(temp, -1); 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(255, 255, 2550, 2550, 255, 255, 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.efFramesToHex(framesList.get(j),framesList.get(j).getGroupIndex(),timeStamp)); } timeStamp++; } return list; }优化此段代码
首先,可以将数组初始化部分的代码优化为使用Java 8的IntStream来进行初始化。
其次,对于portMap的初始化和添加部分,可以使用Java 8的Stream API中的filter和map方法来实现,可以简化代码。
最后,对于framesList的排序部分,可以使用Collections.sort方法代替stream中的sorted方法,因为stream中的sorted方法需要创建一个新的List对象,而Collections.sort方法可以直接对原List进行排序。
优化后的代码如下:
```
private List<String> efJsonToBinList(List<LightGroupReq> lightGroupReqs) {
LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs, VehicleConstants.VEHICLE_009);
List<String> list = new ArrayList<>();
Map<Integer, Frames> portMap = new HashMap<>();
List<Frames> frameList = new ArrayList<>();
IntStream.rangeClosed(1, 48).forEach(i -> frameList.add(new Frames(255, 255, 2550, 2550, 255, 255, 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[49];
Arrays.fill(temp, -1);
framesList.forEach(f -> temp[f.getGroupIndex()] = 1);
portMap = IntStream.rangeClosed(1, temp.length - 1)
.filter(i -> temp[i] == -1)
.mapToObj(i -> new Frames(255, 255, 2550, 2550, 255, 255, 0, i, 0, 2))
.collect(Collectors.toMap(Frames::getGroupIndex, f -> f, (a, b) -> a, HashMap::new));
framesList.addAll(portMap.values());
Collections.sort(framesList, Comparator.comparing(Frames::getGroupIndex));
portMap.clear();
if (timeStamp > 7) {
timeStamp = 0;
}
framesList.forEach(f -> list.add(BinaryFileUtils.efFramesToHex(f, f.getGroupIndex(), timeStamp)));
timeStamp++;
}
return list;
}
```